简体   繁体   English

从UITabbarItem和Storyboard创建一个UIViewController

[英]Create a UIViewController from UITabbarItem and Storyboard

I am trying to open a UIViewController if I press the second UITabbarItem. 如果我按第二个UITabbarItem,则尝试打开UIViewController。

Here is my code of the first ViewController. 这是我第一个ViewController的代码。 It sets the Label.text, if I press UITabbarItem #1 (Tag 1). 如果我按UITabbarItem#1(标签1),它将设置Label.text。 At #2 I want to switch to the secondViewController and (Tag 2) it crashes. 在#2,我想切换到secondViewController并使其崩溃(标签2)。 Anyone knows why? 有人知道为什么吗? First Breakpoint is at UIStoryboard... (Storyboard is called: Main.storyboard). 第一个断点位于UIStoryboard ...(Storyboard称为:Main.storyboard)。 Thanks for help 感谢帮助

#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

错误

There are a few things. 有几件事。

  1. if not already make a property for the UITabBarController 如果尚未为UITabBarController设置属性

  2. Unless you want to navigate away from the UITabBarController you do not push the view controller, you would have to either manually set the view controller to the tabBar item or re-set the view controllers array with in the tabBarController. 除非您不想离开UITabBarController导航,否则不要按下视图控制器,否则必须将视图控制器手动设置为tabBar项,或者在tabBarController中重新设置视图控制器数组。

    NSMutableArray* newControllers = [NSMutableArray arrayWithArray:self.tabBarController.viewControllers]; NSMutableArray * newControllers = [NSMutableArray arrayWithArray:self.tabBarController.viewControllers];

    [newControllers addObject:second]; [newControllers addObject:second];

    [self.tabBarController setViewControllers:newControllers animated:NO]; [self.tabBarController setViewControllers:newControllers animation:NO];

  3. Your UITabBarController is in a storyboard? 您的UITabBarController在情节提要中吗? hold the right click button over the tab you want and drag it over the controller, then set that controller as the location for the tab, if you do that you can ignore 1 and 2 above 在所需的选项卡上按住鼠标右键并将其拖动到控制器上,然后将该控制器设置为选项卡的位置,如果这样做,则可以忽略上面的1和2

  4. UITabBarController are not meant to do different functionality per tab, so that's a bad coding practice to have the first tab set a label and the second tab display an UIViewController , but that is irrelevant. UITabBarController并不意味着每个选项卡都具有不同的功能,因此将第一个选项卡设置为标签,而第二个选项卡显示UIViewController则是不正确的编码做法,但这是不相关的。

After all of that UITabBarController stuff, the app is possibly crashing because your UINavigationController is nil, or you get a "SecondView is not value coding compliant error" or something similiar, this means that a property is set in a storyboard to an item that no longer exist. 在所有这些UITabBarController之后,该应用程序可能会崩溃,因为您的UINavigationController为nil,或者您收到“ SecondView不是值编码兼容错误”或类似内容,这意味着在情节提要中将属性设置为没有不再存在。 So with in the storyboard right click the the view controller and see if you see any yellow warning markers, if so hit the "X" next to them all. 因此,在情节提要中右键单击视图控制器,查看是否看到任何黄色警告标记,如果是,请单击所有警告标记旁边的“ X”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM