用户在 didSelectRowAt 方法中选择原始数据后如何显示视图控制器 4 秒

Posted

技术标签:

【中文标题】用户在 didSelectRowAt 方法中选择原始数据后如何显示视图控制器 4 秒【英文标题】:How to show view controller for 4 seconds after user select the raw in didSelectRowAt method 【发布时间】:2021-12-29 13:11:33 【问题描述】:

我有一个带有表格视图的视图控制器。我想知道如何做到,当用户点击其中一行时,它将显示另一个视图控制器 5 秒。这是我的代码。我创建了一个 objc 函数:

@objc func moveHome() 
        store.dispatch(NavigationAction(destination: .home, direction: .forward))
    

并且在方法 didSelectRowAt:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) 
        let st = UIStoryboard(name: "SchoolsMain", bundle: nil)
        let vc = st.instantiateViewController(withIdentifier: "SchoolsPreHomeViewController") as? SchoolsPreHomeViewController
        UserDefaults.standard.set(testData[indexPath.row].name, forKey: "orgNameForSplash")

        self.timer = Timer.scheduledTimer(timeInterval: 5, target: self, selector: #selector(self.moveHome), userInfo: nil, repeats: false)

    

这是有效的,但不像我想要的那样。现在,当我们选择每一行时,我们等待 5 秒,然后显示另一个视图控制器。但我希望我们点击它,它会在此刻显示 5 秒。

【问题讨论】:

你想展示 SchoolsPreHomeViewController吗?或者,您的表格视图是否位于导航控制器中并且您想要推送它? 我想像闪屏一样显示它,然后转到另一个视图控制器 这是一个令人困惑的描述...当您选择一行时,您想要,什么,显示一个 全屏视图 5 秒?然后,您想“转到另一个视图控制器” ...“转到”是什么意思?你在导航控制器中吗? 【参考方案1】:

你可以这样做。

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) 
    let st = UIStoryboard(name: "SchoolsMain", bundle: nil)
    gaurd let vc = st.instantiateViewController(withIdentifier: "SchoolsPreHomeViewController") as? SchoolsPreHomeViewController else 
        return
    
    present(vc, animated: true, completion: nil)
    // or you can push it to the navigation controller
    DispatchQueue.main.asyncAfter(deadline: .now() + 5.0) 
        vc.dismiss(animated: true, completion: nil)
        // if you have pushed the vc on navigation stack then pop it here
    



【讨论】:

所以关闭后我可以显示另一个视图控制器?【参考方案2】:

在您需要显示的 ViewController (SchoolsPreHomeViewController) 中设置 5 秒的计时器,并在这 5 秒完成时调用您需要编写代码以将其关闭/拉出的方法 (SchoolsPreHomeViewController)。

如有任何疑问,请随时询问。

最新编辑 SchoolsPreHomeViewController.swift

class SchoolsPreHomeViewController: UIViewController 
    
    var timer: Timer?
    
    override func viewDidLoad() 
        super.viewDidLoad()
        timer = Timer.scheduledTimer(timeInterval: 4, target: self, selector: #selector(moveHome), userInfo: nil, repeats: false)
    
    
    @objc func moveHome() 
        // Assuming your desired view controller is in 'SchoolsMain' storyboard and its class name is 'HomeViewController'
        let st = UIStoryboard(name: "SchoolsMain", bundle: nil)
        let vc = st.instantiateViewController(withIdentifier: "HomeViewController") as? HomeViewController
        self.navigationController?.pushViewController(vc, animated: true)
    

【讨论】:

我在schoolsprehomeviewcontroller中尝试过这样的事情,但它不起作用:覆盖func viewDidAppear(_动画:Bool)SchoolsPreHomeViewController.screenPresnted = true timer = Timer.scheduledTimer(timeInterval:4,目标:自我,选择器:#selector(moveHome),userInfo:无,重复:false)@objc func moveHome() store.dispatch(NavigationAction(目的地:.appHome,方向:.forward)) 您可以在 moveHome() 中尝试使用 'self.navigationController?.popViewController(animated: true)' 而不是 'store.dispatch(NavigationAction(destination: .appHome, direction: .forward))'功能。 我试过了,它把我带回了选择行的视图控制器 哦,好吧,那么你想导航到哪个视图控制器? 我希望用户选择它应该使用schoolprehomeviewcontroller 显示初始屏幕的行并转到另一个

以上是关于用户在 didSelectRowAt 方法中选择原始数据后如何显示视图控制器 4 秒的主要内容,如果未能解决你的问题,请参考以下文章

为自定义单元格调用 didSelectRowAt 索引

我想使用 didSelectRowAt 将数组从 UITableView 显示到下一个 viewController

带有 didselectrowAt 索引的 UiTableview 排序返回先前的选择?

未调用 UiTableView didSelectRowAt 方法

TableView didSelectRowAt IndexPath 没有被调用

无法让 didSelectRowAt 为 TableView 工作