我创建了一个包含4个viewcontroller及其.h,.m文件的应用程序...在我的firstviewcontroller中按下一个按钮,它转到secondviewcontroller,在第二个viewcontroller中有两个按钮,用于切换回firstviewcontroller另一个按钮将转到thirdviewcontroller。我的代码是firstviewcontroller.m
[[NSBundle mainBundle] loadNibNamed:@"SecondViewController" owner:self options:nil];
并在我的secondviewcontroller中获取第一个按钮
[[NSBundle mainBundle] loadNibNamed:@"FirstViewController" owner:self options:nil];
和另一个按钮
[[NSBundle mainBundle] loadNibNamed:@"ThirdViewController" owner:self options:nil];
当我在firstviewcontroller中选择按钮时,它会加载secondviewcontroller,但在第二个视图控制器中,如果我选择任何按钮,我会收到Sigabart警告......
任何人都可以对此有所了解......我已经尝试了很多方法......
答案 0 :(得分:4)
您可以使用以下方法执行这些任务:
方法1:
在FirstViewController.m中,点击按钮编写此代码:
SecondViewController *secondVC = [[SecondViewController alloc]initwithNibName:@"SecondViewController" bundle:nil];
[self.view addSubView:secondVC.view];
这会将secondviewcontroller添加到当前视图
在SecondViewController.m中添加第三个View,你可以写
ThirdViewController *thirdVC = [[ThirdViewController alloc]initwithNibName:@"ThirdViewController" bundle:nil];
[self.view addSubView:thirdVC.view];
并删除第二个视图,您可以写下:
[self.view removeFromSuperview];
方法2:
使用导航控制器。
答案 1 :(得分:2)
使用这样的代码
FirstViewController *FVC = [[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil];
//For Push
[self.navigationController pushViewController:FVC animated:YES];
//For Pop
[self.navigationController popViewControllerAnimated:YES];
:)
答案 2 :(得分:1)
在iPhone应用程序编程中,当我们将视图控制器推送到新的另一个视图控制器时,viewControllers通常逐个存储在堆栈中。和视图在相同的反向格式中删除这些控制器。所以如果你想推新的视图控制器,请使用:
SecondViewController * second = [[SecondViewController alloc] initWithNibName:@“SecondViewController”bundle:nil];
[self.navigationController pushViewController:second animated:YES];
如果你想删除当前的视图控制器,请使用:
`[self.navigationController popViewControllerAnimated:YES];
如果你想从任何一个视图控制器跳转到你的第一个视图控制器
[self.navigationController popToRootViewControllerAnimated:YES];
您可以通过这行代码
获取此堆栈中显示的viewcontrollersNSArray * viewArr = [self.navigationController viewControllers];
的NSLog(@ “%@”,viewArr);
[self.navigationController popToViewController:[viewArr objectAtIndex:1] animated:YES];
`
答案 3 :(得分:1)
//For going in forword direction you can use this block
{
FirstViewController *firstVC = [[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil];
[self.navigationController pushViewController:firstVC animated:YES];
[firstVC relese];
}
//For returning back you can use this block
{
[self.navigationController popViewControllerAnimated:YES];
}
//But before using this line of code you have to alloc and make the property of navigationController in your appDelegate
希望这会对你有所帮助