如何从故事板中的子视图(另一个视图控制器)中的按钮推送新的视图控制器 [英] How can I push a new View Controller from a button within a subView (another View Controller) in Storyboard

查看:23
本文介绍了如何从故事板中的子视图(另一个视图控制器)中的按钮推送新的视图控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

  2. 我尝试使用以下代码访问它:

    AppDelegate *del = (AppDelegate *)[UIApplication sharedApplication].delegate;UINavigationController *navigationController = (UINavigationController*)del.window.rootViewController;DetalleMisMarcas *detalleMarcas = [[DetalleMisMarcas alloc] initWithNibName:@"DetalleMarcas" bundle:nil];[navigationController pushViewController:detalleMarcas 动画:YES];

但它不起作用.

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

最后,我加载 3 个子视图的方式在这里:

 -(IBAction)segmentCtrlChanged:(id)sender {UISegmentedControl *seg = 发送者;如果(seg.selectedSegmentIndex == 0){MMViewController *mm= [self.storyboard instantiateViewControllerWithIdentifier:@"MMView"];mm.view.frame = CGRectMake(0, 0, 320, 240);[contentView addSubview:mm.view];}否则如果(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);}

有什么想法吗?

解决方案

根据您的评论,我认为委托会更好地工作.这就是你可以实现它的方式.假设在 contentView 控制器类中,添加一个委托

<块引用>

//在头文件中@property (weak, nonatomic) id 代表;@protocol ContentViewControllerDelegate - (void)contentViewDidSelectAView:(UIView *)view;@结尾//在实现中- (IBAction)segmentCtrlChanged:(UISegmentedControl *)sender {案例0:[self.delegate contentViewDidSelectAView:[[self.storyboard instantiateViewControllerWithIdentifier:@"MMView"] 视图];休息;//和其余的代码}//在主视图头文件中@interface MainViewController : UIViewController //在 MainView 实现中- (void)contentViewDidSelectAView:(UIView *)view {[self.view addSubView:view];}

我还没有真正实现上面的代码.但我认为这应该是控制器与其父级对话的方式.希望这个有帮助.

<小时>

我不确定您到底在做什么,但假设您在根目录中有一个 UINavigationController,并且 mainViewController 通过 segue 作为关系链接.正如您已经实现的那样,mainVC 中的 segmentedControl 应该通过 valueChange 事件链接到 -(IBAction)segmentCtrlChanged:(id)sender.在这种方法中,您可以切换索引,而不是使用 if,虽然它们的工作方式相同,但您也可以使用 UISegmentedControl * 代替 id :

<块引用>

-(IBAction)segmentCtrlChanged:(UISegmentedControl *)sender {开关(sender.selectedSegmentIndex){案例0:{MMViewController *mm= [self.storyboard instantiateViewControllerWithIdentifier:@"MMView"];[self.navigationController pushViewController:mm 动画:YES];休息;}情况1: {MPViewController *mp = [self.storyboard instantiateViewControllerWithIdentifier:@"MPView"];[self.navigationController pushViewController:mp 动画:YES];休息;}情况 2:{//类似上面休息;}默认:休息;}

vc1、vc2 和 vc3——我假设——是 UIViewController 的实例.因此,请确保它们的类是链接的,并且它们的标识符设置正确.然后,让它们保持原样,因为您不会单独为它们做任何调整.它们将通过代码压入堆栈.

希望有帮助.

Wow, I gave a big thought on the question!

So I have a view Controller called "ContentView" within another view Controller (Main VC). The Main VC has a Navigation Controller which was created using Storyboards. And the contentView loads 3 different view controllers (vc1, vc2 and vc3) depending on the options that a Segmented Control has. So the question now is:

How can I load a new View Controller from the button within one of the subviews (vc2) that will appear once the user selects the option from the segmented control?

  1. Although I have the visible view controller (vc2) in my storyboard, obviously I cannot connect an action to the button to the vc2' file's owner since the Nav Controller is on the Main VC.

  2. I tried to access it with the following code:

    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];
    

But it does not work.

I have tried to find a solution from this forum, but I had no luck. Most consider the existence of a Main Window Xcode 4.2 does not have.

Finally, the way I load the 3 subviews, is here:

    -(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);

}

Any ideas?

解决方案

EDIT: Based on your comment, I think delegates will work better. This is how you may implement it. Let's say in contentView controller class, add a delegate

// 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];
}

I haven't actually implemented the above code. But I think this should be the way for a controller to talk to its parent. Hope this one helps.


I am not sure what exactly you are doing but let's say you have a UINavigationController in the root, and mainViewController is linked via segue as a relationship. As you have implemented, the segmentedControl in mainVC should be linked to -(IBAction)segmentCtrlChanged:(id)sender with valueChange event. In this method you can switch the index, rather than using if, although they work the same, and you can also use UISegmentedControl * instead of 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 and vc3, -- I assume -- are instances of UIViewController. So make sure that their class is linked, and that their identifiers are set properly. and then, just leave them as they are, because you are not going to make any segue for them individually. They are going to be pushed to the stack via code.

Hope it helps.

这篇关于如何从故事板中的子视图(另一个视图控制器)中的按钮推送新的视图控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆