简体   繁体   中英

Create a UIViewController from UITabbarItem and Storyboard

I am trying to open a UIViewController if I press the second UITabbarItem.

Here is my code of the first ViewController. It sets the Label.text, if I press UITabbarItem #1 (Tag 1). At #2 I want to switch to the secondViewController and (Tag 2) it crashes. Anyone knows why? First Breakpoint is at UIStoryboard... (Storyboard is called: 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

  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.

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

    [newControllers addObject:second];

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

  3. Your UITabBarController is in a storyboard? 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

  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.

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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