How to auto-run in Xcode 6 using Swift?

I want to automatically switch from the main viewController to the second view controller after a specified period of time after loading the application.

How can I do it?

Does this need to be done programmatically?

+7
ios8 swift xcode6 segue
source share
4 answers

If your user interface is hosted on a storyboard, you can set NSTimer to viewDidLoad your first ViewController , and then call performSegueWIthIdentifier when the timer fires:

 class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. let timer = NSTimer.scheduledTimerWithTimeInterval(8.0, target: self, selector: #selector(timeToMoveOn), userInfo: nil, repeats: false) } func timeToMoveOn() { self.performSegueWithIdentifier("goToMainUI", sender: self) } 

Here's how you configure segue in the storyboard:

  • Drag the control from the File Owner icon of the first ViewController to the second ViewController .
  • Select "modal" from the popup.

Seting up the segue


  • Click the segue arrow that appears between view controllers. In the Attributes Inspector for segue ...
  • Assign your identifier.
  • Disable Animation if you do not want the screen to slide.

enter image description here

+18
source share

You can use this piece of code:

 let delay = 1 // Seconds dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(delay * NSEC_PER_SEC)), dispatch_get_main_queue()) { self.launchMainUI() return } 

which executes launchMainUI after delay seconds. Replace it with your own implementation, where you instantiate your view controller and represent it, or just call segue.

+1
source share

In your action you should write, as in this example

self.performSegueWithIdentifier ("segue name", sender: self)

after executing this method

 override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { if(segue.identifier == "name of segue") { var view : yourviewcontroller = segue.destinationViewController as yourviewcontroller } } 
0
source share

Swift 4:

 let timer = Timer.scheduledTimer(timeInterval: 8.0, target: self, selector: #selector(segueToSignIn), userInfo: nil, repeats: false) @objc func segueToSignIn() { self.performSegue(withIdentifier: "SignInSegue", sender: self) } 
0
source share

All Articles