gpt4 book ai didi

ios - 无法从单个 UIButton 加载不同的 View Controller

转载 作者:行者123 更新时间:2023-11-29 03:06:21 24 4
gpt4 key购买 nike

我正在尝试使用单个按钮来加载不同的 View Controller 开始按钮的操作如下:

-(IBAction)startApp:(id)sender{
switch (menuItem) {
case 1:
[self openPage1];

break;
case 2:
[self openPage2];

break;
case 3:
[self openPage3];

break;
case 4:
[self openPage4];

break;
default:
break;
}

}

menuItem的值在其他地方设置,没有问题。switch case 中的函数也能在不同的 menuItem 值上正确调用。

-(void)openPage1{
Page1 * myPage1 = [[Page1 alloc] init];
[self.navigationController pushViewController:myPage1 animated:YES];

}

-(void)openPage2{
Page2 *myPage2 = [[Page2 alloc] init];
[self.navigationController pushViewController:myPage2 animated:YES];

}

-(void)openPage3{
Page3 *myPage3 = [[TableViewController alloc]init];
[self.navigationController pushViewController:myPage3 animated:YES];
}

-(void)openPage4{
Page4 *myPage4 = [[Page4 alloc]init];
[self.navigationController pushViewController:myPage4 animated:YES];
}

我导入了 View Controller 的所有 .h 文件,所以这不是问题。

问题是所有 openPage#() 方法都在执行,但页面(即 View Controller )在按下开始按钮时并未加载。

最佳答案

View Controller 真的有导航 Controller 吗?在其中一个 openPage# 方法中放置一个调试点,并检查 self.navigationController 是否为 nil。

当调试器在方法中停止时,您可以通过在控制台中键入 po self.navigationController 来执行此操作。

如果您没有在导航 Controller 中嵌入 View Controller ,则不能使用 navigationController 属性。如果为零,则执行 2 项操作中的 1 项:


使用 [self presentViewController:myPage# animated:YES]; 这将使 View Controller 以模态方式向上动画。


将您当前的 View Controller 放入导航 Controller 中。我不知道您当前的 View Controller 是如何呈现的,但基本上不是只呈现 View Controller ,而是首先将它包装在 UINavigationController 中。假设此 View Controller 是在应用程序委托(delegate)中设置的 Root View Controller :

MyViewController *myController = [MyViewController new];
UINavigationController *navController = [[UINavigationController alloc]
initWithRootViewController:navController];
self.window.rootViewController = navController;

关于ios - 无法从单个 UIButton 加载不同的 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22736168/

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