今天在尝试用storyboard做UITabBarController和UI Nav i ga ti o nUINavigationCotroller的界面,也就是顶部有导航,底部有标签页的应用界面。由于将要做的项目很急,而且开发人员不足,不打算用手写代码的方式,所以考虑尽量使用storyboard去创建界面。
界面如下所示:
UITabBarController作为第一个Controller,分别连接3个UINavigationController,这3个UINavigationController再各自连接3个UIViewController,其中,点击第二个UIViewController导航栏右侧的item会跳转到一个新的UIViewController,效果如下:
现在要实现点击button按钮跳转回第二个标签页的功能,因此为button按钮设置segue,连接到他的上一个UIViewcontroller,如下图所示:
设置后,发现导航和标签栏都没有了,代码运行后效果如下:
当点击button后,返回到了上一个页面,但是同时导航和标签也不见了。
在stack overflow上找到一个解决办法,将button的segue指向UITabBarController ,也就是第一个controller,为segue设置一个identifier,然后在SegueViewController中添加如下代码:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:@"backsegue"]) {
UITabBarController *destController = [segue destinationViewController];
[destController setSelectedIndex:1];
}
}
在为button设置segue到UITabBarController后,点击button会跳转到首页,但我们需要显示第二页,因此需要在prepareForSegue中将第二页设置为被选中页。
修改后的storyboard为:
程序运行效果如下: