-3

I want to switch to another view controller after my app is loaded after some seconds, how to do that ?

Srinidhi Kowshik
  • 21
  • 1
  • 2
  • 4
  • [How to segue programmatically using swift](https://stackoverflow.com/questions/27604192/ios-how-to-segue-programmatically-using-swift) may also be useful. – NobodyNada Jul 31 '17 at 04:53
  • Please show what you have done until, and there are so many questions related to navigation from one view to another so let us know what you have tried yet so we can help you further. – Pankaj K. Jul 31 '17 at 06:05
  • i have creater 2nd view controller with id as SecondViewController , i need to move to second view controller after some timeframe(1 sec), that is after the app is loaded in 1st view controller,1st view controller is initial view controller – Srinidhi Kowshik Jul 31 '17 at 06:11

5 Answers5

3

Try this

You can perform segue, navigate and present next view controller in the block

Add this in ViewDidLoad , ViewWillAppear

 DispatchQueue.main.asyncAfter(deadline: .now() + 2, execute: {
      self.performSegue(withIdentifier: "leadingToTutorial", sender: self)
 }) 
Harshal Valanda
  • 5,331
  • 26
  • 63
2

In Swift 3.0, you used Time to move one ViewController to second ViewController. here I have create code so watch it.

import UIKit

class ViewController: UIViewController {

    var gameTimer: Timer! //Timer object

    override func viewDidLoad() {

        super.viewDidLoad()

        gameTimer = Timer.scheduledTimer(timeInterval: 0.5, target: self, selector: #selector(timeaction), userInfo: nil, repeats: true)

    }

    //Timer action
    func timeaction(){

        //code for move next VC
        let secondVC = storyboard?.instantiateViewController(withIdentifier: "SecondViewController") as! SecondViewController
                self.navigationController?.pushViewController(secondVC, animated: true)
        gameTimer.invalidate()//after that timer invalid

    }
}

I hope It's work.

Berlin
  • 2,115
  • 2
  • 16
  • 28
0

when viewDidLoad Method of your First ViewControllers will Execute , you can define there your code For Move to Another ViewController , For Some Second delay you can use

For Delay of Some Seconds

DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
    // your code here For Pushing to Another Screen
}

here you can define how much time you want as a delay , in example i mentioned 0.1 , you can define as per your requirements

Er.Gopal Vasani
  • 448
  • 3
  • 12
0

Use the Timer

Class name {
  private var timer: Timer?

  fun viewDidLoad() {
        timer = Timer.scheduledTimer(timeInterval: 5, target: self, selector: #selector(yourMethod), userInfo: nil, repeats: false)

  }

  objc func yourMethod() {
   performSegue(withIdentifier:Sender:)
  }
}
0

In your view's viewDidLoad() method you can add following line at the end of the method

[self performSelector:@selector(goNext:)withObject:nil afterDelay:5.0];

And inside goNext: method you can add

[self.navigationController pushViewController:vc animated:YES];

to perform navigation and go to next view.

Paras Gorasiya
  • 1,295
  • 2
  • 13
  • 33