Replace tab and click view from AppDelegate

I have a method called handleLocalNotification in my AppDelegate that runs when my application receives a notification. I need it to switch to tab 0 in my UITabBarController which contains a UITableview. Then I need it to click on the correct row of the table to show the record sent by the notification. All controllers are created in the storyboard, so I have no links to them in AppDelegate.

I added to my AppDelegate.h:

@class MyListViewController;
@interface iS2MAppDelegate : UIResponder <UIApplicationDelegate> {

    MyListViewController *_listControl;

}

and for testing, I just put 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 the way I can get it to load on different tabs. The last 2 lines lead to failure. Did I get it right? Or do I need to use a different method? The reason for the failure:

UINavigationController tableView]: ,

+3
1

NSNotificationCenter ,

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
  UITabBarController *tabb = (UITabBarController *)self.window.rootViewController;
  tabb.selectedIndex = 0;
 [[NSNotificationCenter defaultCenter] postNotificationName:@"localNotificationReceived" object:nil];
}

Controller viewDidLoad:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(selectRows) name:@"localNotificationReceived" object:nil];

tableView

-(void) selectRows
{
    [tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:4 inSection:0] animated:YES scrollPosition:UITableViewScrollPositionTop];
}

, :

NSIndexPath *selectedCellIndexPath = [NSIndexPath indexPathForRow:4 inSection:0];
[table selectRowAtIndexPath:selectedCellIndexPath 
                   animated:YES 
             scrollPosition:UITableViewScrollPositionTop];
[table.delegate tableView:table didSelectRowAtIndexPath:selectedCellIndexPath];

selectRowAtIndexPath , .

+5

All Articles