简体   繁体   中英

Created TabbarController in Storyboard can't change view programmatically

Created a tabbarcontroller as a detail view of splitview controller. I can change the view by clicking item1 , item2 icons on simulator, but can not change view programmatically.

I am getting null when try to print viewcontrollers in nslog . In MasterView:

@property (strong, nonatomic) TabBarRootViewController *detailViewController;
- (void)viewDidLoad
{
    [super viewDidLoad];
    self.detailViewController=[[TabBarRootViewController alloc] init];
//tried also
self.detailViewController = (TabBarRootViewController *)[self.splitViewController.viewControllers objectAtIndex:1];

}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //this sends object info to detail
    if (indexPath.section==0) {
        //send row number
       NSNumber *i = [NSNumber numberWithInteger:indexPath.row];
        NSLog(@"Selected index %@",i);

        self.detailViewController.detailItem = i;
    }
}

In detail(Tabbar):

@property (strong, nonatomic) id detailItem;

if (self.detailItem) {
    NSInteger i=[self.detailItem integerValue];
    NSLog(@"recieved integer is %i",i);

    //tried this
    self.tabBarController.selectedIndex=i;
    self.tabBarController.selectedViewController = [self.tabBarController.viewControllers objectAtIndex:i];

    //list of viewcontrollers
    NSArray *array;
    array = [[self tabBarController] viewControllers];
    NSLog(@"array %@",array);

}

在此处输入图片说明

NSLOG:
recieved integer is 1
array (null)

How can I change the view programmatically?

Thanks,

S

Looks like your tab bar controller is nil . Maybe not correctly linked with storyboard?

You need to use performSegueWithIdentifier:sender: on your current controller.

This is because your controller is now controlled by the storyboard and it must maintain state etc.

Note that you need to give your segues an ID in the storyboard editor and you cannot use your own inits, instead you have to override prepareForSegue:sender: to inject properties.

Good luck.

The problem was not able to get the exact pointer to theTabbarcontroller.

I removed the navigation controller and left only tabbar controller.Also removed the [topcontroller] requests in master view and appdelegate.

So final working code is

appdelegate:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.
    return YES;
}

masterview: - (void)viewDidLoad

{
    [super viewDidLoad];

    self.detailViewController = (TabBarRootViewController *)[self.splitViewController.viewControllers objectAtIndex:1];

}

in tabbarcontroller (detail controller)

@property (strong,nonatomic) UITabBarController *rootController;

        self.rootController= (TabBarRootViewController *)[self.splitViewController.viewControllers objectAtIndex:1];
        self.rootController.selectedIndex=i;

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