我有一个基于标签栏的应用程序。所有5个选项卡中都有导航控制器,自定义视图控制器实例正确设置为根视图控制器。加载就好了。其中一些视图控制器包含表视图。我想在用户在表视图中选择一行时向用户显示模态视图控制器。 didSelectRowAtIndexPath委托方法的(相关部分)如下所示:
SampleSelectorViewController *sampleVC = [[SampleSelectorViewController alloc] init];
[self presentViewController:sampleVC animated:YES completion:NULL];
模态视图控制器出现但是在非常明显的延迟之后出现。有时它甚至要求用户第二次点击该行。我已经验证过的一些事情是:
有什么想法/建议吗?
似乎从presentViewController:animated:completion
内部调用tableView:didSelectRowAtIndexPath:
是有问题的。在仪器中使用Time Profiler时也很难找到任何突出的东西。有时我的模态视图会在不到一秒的时间内出现,有时则需要4s甚至9s。
我认为这与底层的UIPresentationController
和布局有关,这是我在选择时间区域和选择时间分析器中的模态显示之间选择时间区域时所看到的少数几项内容之一。
雷达存在描述这个问题:http://openradar.appspot.com/19563577
解决方法很简单但不满意:稍微延迟演示,以避免引起减速的任何争议行为。
dispatch_async(dispatch_get_main_queue(), ^{
[self presentViewController:nav animated:YES completion:nil];
});
我想你也将单元格的selectionStyle设置为UITableViewCellSelectionStyleNone
。我改为UITableViewCellSelectionStyleDefault
,它完美无缺。
Swift 4:你可以使用如下。
DispatchQueue.main.async {
let popUpVc = Utilities.viewController(name: "TwoBtnPopUpViewController", onStoryboard: "Login") as? TwoBtnPopUpViewController
self.present(popUpVc!, animated: true, completion: nil)
}
这个对我有用。
您应该从根vc以模态方式显示它(例如:customTabBarRootViewController)。保存引用,并使用引用控制器显示它。
当我从tableView:didSelectRowAtIndexPath:
呈现看起来像苹果虫时,我也有这种奇怪的延迟。
这个解决方案似乎运作良好。
CFRunLoopWakeUp(CFRunLoopGetCurrent()); // Fixes a bug where the main thread may be asleep, especially when using UITableViewCellSelectionStyleNone
如果你在tableView(:didSelectRowAt :)中调用present(:animated:completion :),为选定的tableview单元格调用selectionStyle == .none,你就会遇到这种奇怪的行为,然后尝试调用tableView.deselectRow(at:animated :) tableView(_:didSelectRowAt :)中的任何操作。
它有帮助吗?
Swift中的解决方案3
在示例选择器ViewController(显示视图控制器)中使用以下代码
DispatchQueue.global(qos: .background).async {
// Write your code
}
此行为的常见问题如下:
一个为tableView中的一个单元格设置selectionStyle = .none
(似乎它不依赖于UITableViewController
编写的http://openradar.appspot.com/19563577子类化)并在委托方法中使用
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
动画取消选择
tableView.deselectRow(at: indexPath, animated: true)
这意味着非动画细胞的动画。
在这种情况下,后续视图控制器呈现具有延迟。
有一些解决方案解决方案,包括主线程上的dispatch_async
,但最好不要在代码中的不可选单元格上调用deselectRow
,即使没有动画也是如此。
根据@ Y.Bonafons评论,在Swift中,你可以尝试这样,(对于Swift 4.x和5.0)
DispatchQueue.main.async {
self.showAction() //Show what you need to present
}