how to move to popToViewController: from tabBarCon

2019-09-05 00:53发布

I have my app is a TabBarController, and I have to take different actions depending on which tab is pressed, so, I delegate TabBarContoller, and I got it.

Now in case a tab which contents a NavigationController is pressed, depending on a database value, the view that should be shown is the first one or the last one, so in Tabs.m:

-(void) tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
 NSArray *viewArrays = [(UINavigationController*)viewController viewControllers];

switch (tabBarController.selectedIndex) {

    case 3:
    {
        NSString *query = @"SELECT * FROM login";
        sqlite3_stmt *statement = [[Database alloc] select:query];

        if(sqlite3_step(statement) == SQLITE_ROW) {
           [(UINavigationController*)viewController popToViewController:[viewArrays objectAtIndex:1] animated:YES];

        }
        sqlite3_finalize(statement);
        break;
    }
    default:
        break;
}

}

The problem is objectAtIndex:1 doesn't exist right now

1条回答
我命由我不由天
2楼-- · 2019-09-05 01:52

You can popView only if you have done pushView atleast once. Print and see the objects inside self.navigationController.viewControllers using NSLog and if it contains some object then give your index appropriately(within the range), i am confused why you are doing (UINavigationController*)viewController, try this instead

[self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:2] animated:YES];
查看更多
登录 后发表回答