【问题标题】:First segue with 2 second delay第一个有 2 秒延迟的 segue
【发布时间】:2020-07-24 01:33:13
【问题描述】:

我在父视图控制器中有一个按钮 (searchButton)。按钮 (searchButtonTapped) 的操作是转到 Result View Controller,如下所示。

@IBAction func searchButtonTapped(_ sender: Any) {
         
        let mainStoryboard = UIStoryboard(name: "SearchResult", bundle: Bundle.main)
        
        guard let resultViewController = mainStoryboard.instantiateViewController(withIdentifier: "ResultViewController") as? ResultViewController else { return }
                
        resultViewController.resultText = initialText
    
        navigationController?.pushViewController(resultViewController, animated: true)
        
    }

当我第一次点击按钮 (searchButton) 时,segue 动作发生但有 2 秒延迟。当我返回父视图控制器并再次点击按钮时,从第二次开始,segue 是平滑的。仅在应用程序启动后的第一个 segue 有两秒钟的延迟。我做错了什么在这里延迟?您的想法将不胜感激。谢谢。

这是子视图控制器 (ResultViewController) 的 viewDidLoad。我试图将“A”打印到“F”以识别导致延迟的步骤,但只要我点击 searchButton,从 A 到 F 的所有内容都会立即打印。然后在打印“F”之后,它会延迟大约 2 秒,然后屏幕才会使用 push segue 更新。

override func viewDidLoad() {
        super.viewDidLoad()

        print("A")

        populateDataArray(resultText.isEmpty)
        resetSelectedItemArray(movingIn: true)

        print("B")

        headerView = listTableView.tableHeaderView as? ResultTableHeaderView
        headerView.bannerImageView.contentMode = .scaleAspectFill
        listTableView.tableHeaderView = nil
        listTableView.addSubview(headerView)
        listTableView.contentInset = UIEdgeInsets(top: tableHeaderViewHeight, left: 0, bottom: 0, right: 0)
        
        print("C")

        initializeHeaderView()
        
        setupRightBarButtonItems()
        setupInformationBar()

        setupTableViewStyle()

        print("D")
        
        navigationItem.setHidesBackButton(true, animated: false)
        
        let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
        layout.minimumInteritemSpacing = 5
        layout.scrollDirection = .horizontal
                
        resultCollectionView.collectionViewLayout = layout

        print("E")
        
        resultCollectionView.delegate = self
        resultCollectionView.dataSource = self
        
        listTableView.delegate = self
        listTableView.dataSource = self
        
        stickyDropDownTableView.delegate = self
        stickyDropDownTableView.dataSource = self
        
        informationDropDownTableView.delegate = self
        informationDropDownTableView.dataSource = self

        print("F")
        
    }

【问题讨论】:

    标签: swift segue push delay


    【解决方案1】:

    当目标视图特别复杂并且肯定需要任何繁重的处理来加载时,通常会发生这种情况,并且需要花费一些时间。但你的看起来不像。所以,你可以在主线程内部执行你的 segue,这将缓解问题。

       DispatchQueue.main.async {
          navigationController?.pushViewController(resultViewController, animated: true) 
    }
            
    
          
    

    【讨论】:

    • 感谢您的评论。我应该在哪里使用上面的代码?当我尝试在 IBAction for searchButton 函数中使用它时,我收到此错误消息:Ambiguous use of 'dispatch_get_main_queue()'
    • 对不起,我写了旧版本的调度。我已经编辑了答案。是的,在您正在执行 segue 的 IBAction 中添加该代码。
    • 谢谢。不幸的是,它没有任何区别......我仍然有第一个 segue 的 2 秒延迟。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-01
    • 2013-11-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多