Reputation: 10398
I have a method called handleLocalNotification in my AppDelegate that fires when my app gets a notification. I need it to switch to tab 0 in my UITabBarController which contains a UITableview. I then need it to push on the correct row of the tableview to show the record sent by the notification. All Controllers are created in the storyboard, so I have no references to them in the AppDelegate.
I've added to my AppDelegate.h:
@class MyListViewController;
@interface iS2MAppDelegate : UIResponder <UIApplicationDelegate> {
MyListViewController *_listControl;
}
and for testing I'm just putting this in the didFinishLaunchingWithOptions method:
UITabBarController *tabb = (UITabBarController *)self.window.rootViewController;
tabb.selectedIndex = 0;
_listControl = [tabb.viewControllers objectAtIndex:0];
[_listControl.tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:4 inSection:0] animated:YES scrollPosition:UITableViewScrollPositionTop];
The tabBarController bit works as I can get it to load up on different tabs. The last 2 lines cause it to crash. Have I gone about this the correct way? Or do I need to use a different method? The crash reason is:
UINavigationController tableView]: unrecognized selector sent to instance
Upvotes: 3
Views: 2953
Reputation: 6160
i suggest to use NSNotificationCenter
try to do it like this
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
UITabBarController *tabb = (UITabBarController *)self.window.rootViewController;
tabb.selectedIndex = 0;
[[NSNotificationCenter defaultCenter] postNotificationName:@"localNotificationReceived" object:nil];
}
and in your viewController viewDidLoad
:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(selectRows) name:@"localNotificationReceived" object:nil];
now you can use your tableView and do your stuff
-(void) selectRows
{
[tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:4 inSection:0] animated:YES scrollPosition:UITableViewScrollPositionTop];
}
To programmatically select a cell , this may do the job :
NSIndexPath *selectedCellIndexPath = [NSIndexPath indexPathForRow:4 inSection:0];
[table selectRowAtIndexPath:selectedCellIndexPath
animated:YES
scrollPosition:UITableViewScrollPositionTop];
[table.delegate tableView:table didSelectRowAtIndexPath:selectedCellIndexPath];
because selectRowAtIndexPath will not fire the table delegate methods so you have to call with your self.
Upvotes: 6