Created TabbarController in Storyboard can't change view programmatically

谁说胖子不能爱 提交于 2020-01-16 19:11:28

问题


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


回答1:


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




回答2:


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.




回答3:


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;


来源:https://stackoverflow.com/questions/15440502/created-tabbarcontroller-in-storyboard-cant-change-view-programmatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!