gpt4 book ai didi

ios - 如何从 Storyboard 中的 subview (另一个 View Controller )中的按钮推送新的 View Controller

转载 作者:行者123 更新时间:2023-11-28 17:38:16 26 4
gpt4 key购买 nike

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

所以我在另一个 View Controller (主 VC)中有一个名为“ContentView”的 View Controller 。主 VC 有一个使用 Storyboard创建的导航 Controller 。并且 contentView 加载 3 个不同的 View Controller (vc1、vc2 和 vc3),具体取决于 Segmented Control 具有的选项。所以现在的问题是:

如何从其中一个 subview (vc2) 中的按钮加载新的 View Controller ,一旦用户从分段控件中选择选项,该 subview 就会出现?

  1. 虽然我的 Storyboard中有可见 View Controller (vc2),但显然我无法将按钮的操作连接到 vc2 文件的所有者,因为导航 Controller 位于主 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 animated:YES];

但它不起作用。

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

最后,我加载 3 个 subview 的方式如下:

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

}

有什么想法吗?

最佳答案

编辑:根据您的评论,我认为代表会工作得更好。这就是您实现它的方式。假设在 contentView Controller 类中,添加一个委托(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];
}

我还没有真正实现上面的代码。但我认为这应该是 Controller 与其父级对话的方式。希望这对您有所帮助。


我不确定你到底在做什么,但假设你在根目录中有一个 UINavigationController,并且 mainViewController 通过 segue 作为关系链接。正如您所实现的那样,mainVC 中的 segmentedControl 应该链接到 -(IBAction)segmentCtrlChanged:(id)sendervalueChange 事件。在此方法中,您可以切换索引,而不是使用 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 的实例。因此,请确保它们的类已链接,并且它们的标识符设置正确。然后,让它们保持原样,因为您不会为它们单独进行任何 segue。它们将通过代码被压入堆栈。

希望对您有所帮助。

关于ios - 如何从 Storyboard 中的 subview (另一个 View Controller )中的按钮推送新的 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9119607/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com