简体   繁体   中英

How to show view controller for 4 seconds after user select the raw in didSelectRowAt method

I have one view controller with table view. I want know, how it possible to make, that when user tap on one of the row it will show another view controller for 5 seconds. Here is my code. I've created a objc func:

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

And in the method 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)

    }

It's works, but not like I want. Now when we select each of the row, we waiting for 5 seconds and then another view controller shows. But I want that we tap and it will show at the moment for 5 seconds.

Set the timer of 5 seconds in the ViewController (SchoolsPreHomeViewController) that you need to show and on the completion of those 5 seconds call the method in which you will need to write the code to dismiss/pull it (SchoolsPreHomeViewController).

Feel free to ask if any doubt.

you can do it like this.

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
    }

}

Agree with A. Basit above. Just want to add that that will only work if you want a modal segue (which you probably do). If you want a slide segue you should replace that with unwind or pop to root nav controller

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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