简体   繁体   English

从TableViewController呈现模态ViewController时滞

[英]Presenting a Modal ViewController from a TableViewController lags

I have UITableViewController that needs to present a view controller modally when a cell is tapped. 我有UITableViewController,需要在轻按单元格时以模态形式显示视图控制器。 I'm using didSelectRowAt for this. 我为此使用didSelectRowAt

The modal view controller is a custom UIViewController subclass that has a loadFromStoryboard() class method to load it. 模态视图控制器是一个自定义UIViewController子类,该子类具有loadFromStoryboard()类方法来加载它。

Sometimes when I tap the cell, the view controller is presented quickly without issue. 有时,当我点击单元格时,视图控制器会快速显示而不会出现问题。 However, other times it doesn't show until I, for instance, try to scroll on the tableview or tap another cell. 但是,有时它直到我尝试在表视图上滚动或点击另一个单元格时才显示。

I'm guessing this is some sort of problem with threading. 我猜想这是线程问题。 However, throughout my entire app, I never delegate a task to another thread or start another queue at all. 但是,在整个应用程序中,我从不将任务委托给另一个线程或根本没有启动另一个队列。

NB: I am using Swift 3. 注意:我正在使用Swift 3。

Update 更新资料

Here's the loadFromStoryboard() method: 这是loadFromStoryboard()方法:

class func loadFromStoryboard(particle: ParticleProtocol, isFavourite: Bool = false) -> ParticleDisplayViewController {
    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    if let viewController = storyboard.instantiateViewController(withIdentifier: "ParticleDisplayViewController") as? ParticleDisplayViewController {

        viewController.particle = particle
        viewController.isFavourite = isFavourite

        return viewController
    } else {
        fatalError("Can't find ParticleDisplayViewController in Main storyboard")
    }
}

And here is didSelectRowAt from my UITableViewController: 这是我的UITableViewController中的didSelectRowAt

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    switch indexPath.section {
    case 1:
        let isFavourite = ParticleStorageManager.standardModelParticleIsFavourite(index: indexPath.row)
        self.present(ParticleDisplayViewController.loadFromStoryboard(particle: standardModelParticles[indexPath.row], isFavourite: isFavourite), animated: true, completion: nil)
    case 0:
        let isFavourite = ParticleStorageManager.savedParticleIsFavourite(index: indexPath.row)
        self.present(ParticleDisplayViewController.loadFromStoryboard(particle: self.particles[indexPath.row], isFavourite: isFavourite), animated: true, completion: nil)
    default:
        self.contextKey = "AddingParticle"
        self.present(CreateParticleTableViewController.loadFromStoryboard(reciever: self), animated: true, completion: nil)
    }

}

ParicleStorageManager simply reads and writes data to the UserDefaults . ParicleStorageManager只需将数据读取并写入UserDefaults

Here's viewDidLoad() from the modal view controller: 这是模态视图控制器中的viewDidLoad()

override func viewDidLoad() {
    self.view.backgroundColor = .clear()

    self.particleViewContainer.layoutIfNeeded()
    self.particleViewContainer.addSubview(ParticleView(position: CGPoint.zero, width: particleViewContainer.width, particle: self.particle, isFavourite: self.isFavourite))

    propertiesTableView.backgroundColor = UIColor.white().withAlphaComponent(0.3)
    propertiesTableView.layer.borderWidth = 1.5
    propertiesTableView.layer.borderColor = UIColor.black().cgColor
    self.propertiesTableView.blur()
}

Calling self.present from within func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) is the reason here as UIPresentationController may take time to find the layout from tableview row selection and causes a delay in presenting a new controller modally. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)内调用self.present是这里的原因,因为UIPresentationController可能需要花费一些时间才能从tableview行选择中查找布局,并导致延迟以模态方式呈现新控制器。

A better way to present is using the DispatchQueue on main thread. 一种更好的呈现方式是在主线程上使用DispatchQueue This way the delay can be avoided. 这样可以避免延迟。

 DispatchQueue.main.async {
           //Your Presentation code should be called here.... 
   }

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

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