[英]Create a UIViewController from UITabbarItem and Storyboard
如果我按第二个UITabbarItem,则尝试打开UIViewController。
这是我第一个ViewController的代码。 如果我按UITabbarItem#1(标签1),它将设置Label.text。 在#2,我想切换到secondViewController并使其崩溃(标签2)。 有人知道为什么吗? 第一个断点位于UIStoryboard ...(Storyboard称为:Main.storyboard)。 感谢帮助
#import "ViewController.h"
#import "secondViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.tabBar.delegate = self;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item{
switch (item.tag) {
case 1: self.testlabel.text =@"test";
break;
case 2:
{ // create the Storyboard object
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
secondViewController *second = [storyboard instantiateViewControllerWithIdentifier:@"secondview"];
[self.navigationController pushViewController:second animated:YES];
}
break;
default: break; }
}
@end
有几件事。
如果尚未为UITabBarController
设置属性
除非您不想离开UITabBarController
导航,否则不要按下视图控制器,否则必须将视图控制器手动设置为tabBar项,或者在tabBarController中重新设置视图控制器数组。
NSMutableArray * newControllers = [NSMutableArray arrayWithArray:self.tabBarController.viewControllers];
[newControllers addObject:second];
[self.tabBarController setViewControllers:newControllers animation:NO];
您的UITabBarController
在情节提要中吗? 在所需的选项卡上按住鼠标右键并将其拖动到控制器上,然后将该控制器设置为选项卡的位置,如果这样做,则可以忽略上面的1和2
UITabBarController
并不意味着每个选项卡都具有不同的功能,因此将第一个选项卡设置为标签,而第二个选项卡显示UIViewController
则是不正确的编码做法,但这是不相关的。
在所有这些UITabBarController
之后,该应用程序可能会崩溃,因为您的UINavigationController
为nil,或者您收到“ SecondView不是值编码兼容错误”或类似内容,这意味着在情节提要中将属性设置为没有不再存在。 因此,在情节提要中右键单击视图控制器,查看是否看到任何黄色警告标记,如果是,请单击所有警告标记旁边的“ X”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.