我有一個帶有表格視圖的視圖控制器。我想知道,如何做到這一點,當用戶點擊其中一行時,它將顯示另一個視圖控制器 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 秒鐘。
uj5u.com熱心網友回復:
你可以這樣做。
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
}
}
uj5u.com熱心網友回復:
在您需要顯示的 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)
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/411955.html
標籤:
上一篇:JavaDate給出不同的日期
