【问题标题】:Change tab and push view from AppDelegate从 AppDelegate 更改选项卡和推送视图
【发布时间】:2012-04-22 11:28:20
【问题描述】:

我的 AppDelegate 中有一个名为 handleLocalNotification 的方法,当我的应用收到通知时会触发该方法。我需要它切换到包含 UITableview 的 UITabBarController 中的选项卡 0。然后我需要它推送表格视图的正确行以显示通知发送的记录。所有控制器都是在情节提要中创建的,所以我在 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]:无法识别的选择器发送到 实例

【问题讨论】:

    标签: objective-c ios xcode uinavigationcontroller uitabbarcontroller


    【解决方案1】:

    我建议使用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 不会触发表委托方法,所以您必须自己调用。

    【讨论】:

    • 谢谢。这是一个很好的解决方法,但是当应用程序完全关闭时不会调用通知观察者。我猜在发送 postNotification 时 tableViewController 还没有加载。如果应用程序只是后台运行,它可以工作。另外,我错误地认为 selectRowAtIndexPath 实际上会为我按下该行,但它只是突出显示了它。有没有办法让它按下特定的行,所以调用 prepareForSegue?
    • 太好了,应该会触发单元格。当应用程序第一次被加载并且观察者在它被调用时还没有设置时怎么办?我可以让它等待并确保在触发通知之前已调用 ViewDidLoad 吗?
    • 嗯...如果您的应用确实收到本地通知,您可以在NSUserDefaults 中设置一个布尔值...如果是,请在视图中检查它是否已加载表视图并将布尔值重置为 NO .. 如果不是,则继续执行标准加载。
    • 非常感谢。一切都与您的建议有关。您知道是否可以在调用 didReceiveLocalNotification 时(仅在应用加载/后台调用时调用)来区分应用是实际在屏幕上使用还是仅在后台使用?
    • 如果您的应用程序处于活动状态,则在这两种情况下都不会调用它。要对这两种情况采取不同的操作,您需要像这样检查应用程序状态:UIApplicationState state = [application applicationState]; if (state == UIApplicationStateInactive) { // Application was in the background when notification was delivered. } else { }
    猜你喜欢
    • 1970-01-01
    • 2015-05-23
    • 2017-10-23
    • 1970-01-01
    • 1970-01-01
    • 2019-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多