skip to Main Content

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.

2

Answers


  1. 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.

    Latest Edit 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)
        }
    }
    
    Login or Signup to reply.
  2. 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
        }
    
    }
    
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search