如何从Storyboard中的子视图(另一个视图控制器)中的按钮推送新的视图控制器

时间:2012-02-02 20:36:09

标签: ios uinavigationcontroller storyboard uiapplicationdelegate addsubview

哇,我对这个问题进行了深思熟虑!

所以我在另一个视图Controller(Main VC)中有一个名为“ContentView”的视图。主VC有一个使用Storyboard创建的导航控制器。并且contentView根据分段控件具有的选项加载3个不同的视图控制器(vc1,vc2和vc3)。所以现在的问题是:

如何从用户从分段控件中选择选项后出现的其中一个子视图(vc2)中的按钮加载新的视图控制器?

  1. 虽然我的故事板中有可见的视图控制器(vc2),显然我无法将操作连接到vc2'文件所有者的按钮,因为导航控制器在主VC上。

    < / LI>
  2. 我尝试使用以下代码访问它:

    AppDelegate *del = (AppDelegate *)[UIApplication sharedApplication].delegate;
    UINavigationController *navigationController =     (UINavigationController*)del.window.rootViewController;
    DetalleMisMarcas *detalleMarcas = [[DetalleMisMarcas alloc] initWithNibName:@"DetalleMarcas" bundle:nil];
    
    [navigationController pushViewController:detalleMarcas animated:YES];
    
  3. 但它不起作用。

    我试图从这个论坛找到解决方案,但我没有运气。大多数人认为Xcode 4.2没有主窗口的存在。

    最后,我加载3个子视图的方式是:

        -(IBAction)segmentCtrlChanged:(id)sender {
    UISegmentedControl *seg = sender;
    if (seg.selectedSegmentIndex == 0)
    {
    
        MMViewController *mm= [self.storyboard instantiateViewControllerWithIdentifier:@"MMView"];
        mm.view.frame = CGRectMake(0, 0, 320, 240);
        [contentView addSubview:mm.view];
    
    }
    
    else if (seg.selectedSegmentIndex == 1) {
        MPViewController *mp = [self.storyboard instantiateViewControllerWithIdentifier:@"MPView"];
        mp.view.frame = CGRectMake(0, 0, 320, 240);
        [contentView addSubview:mp.view];
    
        }
    }
    
    -(IBAction)mainSubView:(id)sender
    {
    MMViewController *mm = [[MMViewController alloc] initWithNibName:@"MTView" bundle:nil];
    [contentView addSubview:theMTView];
    mm.view.frame = CGRectMake(0, 0, 320, 240);
    
    }
    

    有什么想法吗?

1 个答案:

答案 0 :(得分:0)

编辑:根据您的评论,我认为代表们会更好地工作。这是你如何实现它。 假设在contentView控制器类中添加一个委托

// in the header file
@property (weak, nonatomic) id <ContentViewControllerDelegate> delegate;
@protocol ContentViewControllerDelegate <NSObject>
- (void)contentViewDidSelectAView:(UIView *)view;
@end

// in the implementation
- (IBAction)segmentCtrlChanged:(UISegmentedControl *)sender {
case 0:
     [self.delegate contentViewDidSelectAView:[[self.storyboard instantiateViewControllerWithIdentifier:@"MMView"] view];
     break;
// and the rest of the code
}

// in MainView header file 
@interface MainViewController : UIViewController <ContentViewControllerDelegate>

// in MainView implementation
- (void)contentViewDidSelectAView:(UIView *)view {
[self.view addSubView:view];
}

我实际上没有实现上面的代码。但我认为这应该是控制器与其父母交谈的方式。希望这个有所帮助。


我不确定你到底在做什么但是假设你在root中有一个UINavigationController,而mainViewController通过segue作为关系链接。正如您所实现的那样,mainVC中的segmentedControl应该与-(IBAction)segmentCtrlChanged:(id)sender事件链接到valueChange。 在此方法中,您可以切换索引,而不是使用if,尽管它们的工作方式相同,您也可以使用UISegmentedControl *代替id

-(IBAction)segmentCtrlChanged:(UISegmentedControl *)sender {
switch(sender.selectedSegmentIndex) {
case 0: {
       MMViewController *mm= [self.storyboard instantiateViewControllerWithIdentifier:@"MMView"];
       [self.navigationController pushViewController:mm animated:YES];
       break;
      }

case 1: {
      MPViewController *mp = [self.storyboard instantiateViewControllerWithIdentifier:@"MPView"];
      [self.navigationController pushViewController:mp animated:YES];
      break;
     }
 case 2: {
       // similar to above
       break;
     }
 default:
     break;

}

vc1,vc2和vc3, - 我假设 - 是UIViewController的实例。因此,请确保他们的类已链接,并且他们的标识符已正确设置。然后,只要保持它们原样,因为你不会单独为它们制造任何障碍。它们将通过代码被推送到堆栈。

希望它有所帮助。

相关问题