Reputation: 4986
I have a UITabBarController template based app where the first tab is a UITableViewController with custom buttons in the cells. I need one button in one cell to call a specific tab in the parent UITabBarController. Im using this code in the didSelectRowAtIndexPath:
case 5:
NSLog(@"Search");
[tableView deselectRowAtIndexPath:indexPath animated:YES];
// Get UITabBar instance and send it message
UITabBarController *tabBar = (UITabBarController*)[self parentViewController];
[tabBar setSelectedIndex:1];
break;
When I tap on this row the app crashes without any log in the console. Im calling this in that UITableViewController which is the first tab in the UITabBarController.
PS I also want to avoid the blue selection on this cell that occurs when the user taps. So I added this code:
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath{
switch (indexPath.row) {
case 1:
return nil;
break;
}
}
But it still selects blue when I tap on it. Could they be related issues?
Upvotes: 0
Views: 734
Reputation: 558
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:NO];
UITabBarController *tabController = (UITabBarController *) UIApplication.sharedApplication.keyWindow.rootViewController;
[tabController setSelectedIndex:1];
}
the first message will deselect the row, avoiding the blue selection.
As your app is tab-based, the root controller of the key window is always an instance of UITabBarController
.
Upvotes: 1