从AppDelegate更改选项卡和推送视图

Dar*_*ren 3 xcode objective-c uitabbarcontroller uinavigationcontroller ios

我的AppDelegate中有一个名为handleLocalNotification的方法,当我的应用程序收到通知时会触发该方法.我需要它切换到包含UITableview的UITabBarController中的tab 0.然后我需要它来推送tableview的正确行以显示通知发送的记录.所有控制器都在故事板中创建,因此我在AppDelegate中没有引用它们.

我已经添加到我的AppDelegate.h:

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

    MyListViewController *_listControl;

}
Run Code Online (Sandbox Code Playgroud)

为了测试我只是把它放在didFinishLaunchingWithOptions方法中:

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];
Run Code Online (Sandbox Code Playgroud)

tabBarController位工作,因为我可以在不同的选项卡上加载它.最后两行导致它崩溃.我是否以正确的方式解决了这个问题?或者我需要使用不同的方法吗?崩溃的原因是:

UINavigationController tableView]:无法识别的选择器发送到实例

Mal*_*ndi 6

我建议NSNotificationCenter尝试这样做

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

并在您的viewController中viewDidLoad:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(selectRows) name:@"localNotificationReceived" object:nil];
Run Code Online (Sandbox Code Playgroud)

现在你可以使用你的tableView并做你的东西

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

要以编程方式选择单元格,这可能会完成以下任务:

NSIndexPath *selectedCellIndexPath = [NSIndexPath indexPathForRow:4 inSection:0];
[table selectRowAtIndexPath:selectedCellIndexPath 
                   animated:YES 
             scrollPosition:UITableViewScrollPositionTop];
[table.delegate tableView:table didSelectRowAtIndexPath:selectedCellIndexPath];
Run Code Online (Sandbox Code Playgroud)

因为selectRowAtIndexPath不会触发表委托方法,所以你必须自己调用.