2

我遇到了一个问题,即在尝试呈现ViewController. UITableViewCell当用户单击需要高级访问权限的时,我试图显示升级警报。在ViewController介绍中,我放置了调试代码:

override func viewDidLoad() {
    super.viewDidLoad()

    println("\(NSDate()) viewDidLoad")
    // Set Navigation Title font and color
    self.navigationController?.navigationBar.titleTextAttributes = [NSFontAttributeName: UIFont(name: "UbuntuCondensed-Regular", size: 22)!,
        NSForegroundColorAttributeName: UIColor.whiteColor()]
    println("\(NSDate()) end of viewDidLoad")
}

override func viewWillAppear(animated: Bool) {
    println("\(NSDate()) before super.viewWillAppear(animated)")
    super.viewWillAppear(animated)
    println("\(NSDate()) after super.viewWillAppear(animated)")
}

override func viewDidAppear(animated: Bool) {
    println("\(NSDate()) before super.viewDidAppear(animated)")
    super.viewDidAppear(animated)

    println("\(NSDate()) after super.viewDidAppear(animated)")
}

println声明导致:

2015-06-23 16:36:54 +0000 viewDidLoad
2015-06-23 16:36:54 +0000 end of viewDidLoad
2015-06-23 16:36:57 +0000 before super.viewWillAppear(animated)
2015-06-23 16:36:57 +0000 after super.viewWillAppear(animated)
2015-06-23 16:36:58 +0000 before super.viewDidAppear(animated)
2015-06-23 16:36:58 +0000 after super.viewDidAppear(animated)

如您所见,在结束viewDidLoad和开始之间有 3 秒的延迟viewWillAppear。我无法弄清楚为什么会发生这种情况。我在 中以编程方式创建视图ViewController,因此这里没有使用情节提要。

这是我必须展示的代码ViewController

// Create the upgrade view contorller
let upgradeVC = UpgradeViewController()
// Set the presentation context
self.providesPresentationContextTransitionStyle = true
self.definesPresentationContext = true
// Set the upgrade controller to be modal over current context
upgradeVC.modalPresentationStyle = UIModalPresentationStyle.OverCurrentContext
// Show the view controller
self.navigationController?.presentViewController(upgradeVC, animated: true, completion: nil)
4

2 回答 2

4

创建和呈现视图控制器应该在主线程上完成。

(当视图更新延迟时,几乎总是意味着您错误地在后台线程上工作。)

于 2015-06-23T17:07:34.367 回答
2

我把它放在那里是为了让人们明白,如果你在viewDidLoadviewDidAppear时间延迟并不是你做错了什么UITableViewController didSelectRowAt

async awaitSwiftsUITableViewController didSelectRowAt回调中显然存在一个隐藏的后台线程错误:

如果您的代码看起来像这样,它可能会在两者之间滞后 2 或 3viewDidLoadviewDidAppear

class ViewControllerList: UITableViewController{
   override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)       
   {
        let storyBoard = UIStoryboard(name: "Display", bundle:nil)
        let displayView = storyBoard.instantiateViewController(withIdentifier: "ViewControllerDisplay") as! ViewControllerDisplay
        self.present(displayView, animated: true, completion: nil)
   }
}

(尽管你没有做任何应得的)

这就是你在 Swift 4 中加速它的方式

class ViewControllerList: UITableViewController{
   override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)       
   {
        DispatchQueue.global(qos: .background).async {
            // Background Thread
            DispatchQueue.main.async {
                // Run UI Updates or call completion block
                let storyBoard = UIStoryboard(name: "Display", bundle:nil)
                let displayView = storyBoard.instantiateViewController(withIdentifier: "ViewControllerDisplay") as! ViewControllerDisplay
                self.present(displayView, animated: true, completion: nil)
            }
        }
   }
}
于 2019-04-03T09:50:55.750 回答