问题
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:
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];
来源:https://stackoverflow.com/questions/12527581/how-to-move-to-poptoviewcontroller-from-tabbarcontroller