繁体   English   中英

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

[英]Change tab and push view from AppDelegate

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

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

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

    MyListViewController *_listControl;

}

为了测试我只是把它放在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];

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

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

我建议使用NSNotificationCenter尝试这样做

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

并在您的viewController 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不会触发表委托方法,所以你必须自己调用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM