How do I perform an auto-segue in Xcode 6 using Swift?

风格不统一 提交于 2019-11-26 16:37:40

问题


I wish to auto-segue from the main viewController to a second view controller after a set period of time after an app loads.

How do I do this?

Do I need to do it programmatically?


回答1:


If your UI is laid out in a Storyboard, you can set an NSTimer in viewDidLoad of 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 = Timer.scheduledTimer(interval: 8.0, target: self, selector: #selector(timeToMoveOn), userInfo: nil, repeats: false)
    }

    @objc func timeToMoveOn() {
        self.performSegue(withIdentifier: "goToMainUI", sender: self)
    }

Here is how you set up the segue in the Storyboard:

  1. Control drag from the File's Owner icon of the first ViewController to the second ViewController.
  2. Choose "modal" from the pop-up.


  1. Click on the segue arrow that appears between the view controllers. In the Attributes Inspector for the segue...
  2. Give your segue an Identifier.
  3. Turn off Animates if you don't want to see the screen slide in.




回答2:


You can use this snippet 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 performs the launchMainUI method after delay seconds. Replace it with your own implementation, where you instantiate your view controller and present it, or simply invoke a segue.




回答3:


In your action you must write like this example

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

after you must implemented this method

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

}



回答4:


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)
}


来源:https://stackoverflow.com/questions/26379462/how-do-i-perform-an-auto-segue-in-xcode-6-using-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!