1

我的 iOS 6 应用程序有一个简单的工作流程:

UINavigationController -> loadingVC -(push)-> UITabBarController -> UIViewController(s)

我调用 push via performSegueWithIdentifier:,使用观察者 on 调用自身NSNotificationCenter,但是加载 UITabBarController 的根 VC 需要太多时间(约 35 秒)。

这是我在 loadingVC 中注册观察者的方式:

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

然后,我的 loadingVC 从我的 AppDelegate 调用一些方法,该方法将继续处理一些网络内容(获取和解析 XML 文件),并等待将触发的通知:

[self performSegueWithIdentifier:@"finishedLoading" sender:self];

这是我在加载根 VC 时得到的控制台日志UITabBarController

2013-05-06 16:28:19.720 *** [2289:19303] Notif sent
2013-05-06 16:28:19.720 *** [2289:19303] Notif received
2013-05-06 16:28:19.727 *** [2289:19303] viewDidLoad in         (some basic stuff)
2013-05-06 16:28:19.728 *** [2289:19303] viewDidLoad out
2013-05-06 16:28:19.729 *** [2289:19303] viewWillAppear         (NSLog only)
2013-05-06 16:28:54.475 *** [2289:19303] In numberOfSections    (datasource method of a UITableview, returns 0)
2013-05-06 16:28:54.832 *** [2289:12b03] viewDidAppear          (NSLog only)

如您所见,viewWillAppear在推送实际发生之前,我有 35 秒的时间。该应用程序挂在 上loadingVC,然后像往常一样推送。

所有这些都发生在模拟器中(我暂时无法在物理设备上进行测试)。

我尝试了什么:

• 我使用SDSegmentedControl, 的子类UISegmentedControl,但我尝试使用后者,同样的问题发生了。

• 我删除了我UINavigationController的并用模态segue 替换了push。它似乎工作得更快,但 UI 完全搞砸了:在 segue 期间没有动画(即使我将其设置为“是”),标签栏不会在段上显示文本或图标,直到你点击它们,我的分段控件不出现。

我似乎无法理解发生了什么。有什么帮助吗?

4

1 回答 1

1

听起来您可能正在后台线程中执行该操作,这很容易发生,具体取决于您的通知来源。UIKit 调用(如-performSegueWithIdentifier:sender:)不是线程安全的,并且在主线程以外的线程上调用时会产生不可预知的结果。

尝试使用以下内容更新方法的开头-loadingDidComplete

-(void)loadingDidComplete {
    if (![NSThread isMainThread]) {
        [self performSelectorOnMainThread:@selector(loadingDidComplete)
                               withObject:nil
                            waitUntilDone:NO];
        return;
    }

    // ... rest of the method
}
于 2013-05-06T15:33:02.467 回答