2016-11-03 27 views
0

我一直在試圖以編程方式執行賽格瑞做:順着接下去編程不起作用

import UIKit 

class ViewController: UIViewController { 


    @IBOutlet weak var btnRedirect: UIButton! 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     // Do any additional setup after loading the view, typically from a nib. 

     self.performSegueWithIdentifier("segueToSecond", sender: btnRedirect) 

    } 

    @IBAction func redirect(sender: AnyObject) { 
     self.performSegueWithIdentifier("segueToSecond", sender: self) 
    } 
} 

的問題是SEGUE通過點擊按鈕來執行它雖然當我嘗試做它完美以編程方式,您可以在viewDidLoad方法中看到,它不起作用!

我已經試過通過sender作爲nil

+2

不能執行'viewDidLoad'一個SEGUE,做到在'viewDidAppear'如果你想盡快做查看屏幕 – dan

+0

我們得到它,你的問題是關於Swift。不需要所有的帽子。 – Alexander

+0

代碼updated.let我知道你的事情... – Joe

回答

0

由於dan'S評論你不能在ViewDidLoad中執行segue在viewDidAppears中做它。

注:移動從您的viewDidLoad執行順着接下去viewDidAppers或您的按鈕操作中如下面的代碼,並確保你設定賽格瑞標識符故事板「segueToSecond」 .Untested code.So,讓我知道。如果你有任何麻煩執行。

更新時間:

注:如果你希望你的看法賽格瑞加載後自動執行。你應該將VC從VC連接到VC。

 @IBAction func redirect(sender: AnyObject) { 
    segueToSecondVC() 
} 


     func segueToSecondVC() { 
     self.performSegue(withIdentifier: "segueToSecond", sender: self) 
    } 

     override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 

     if (segue.identifier == "segueToSecond") { 

     let dest = segue.destination as! viewTwo // viewTwo is your destination ViewController 
     print("Segue Performed") 

    } 

    } 

更新時間:

注:如果你想SEGUE你可以設置從下面的代碼有一些延遲,以您的觀點賽格瑞加載後automatilcally執行。

 let delayInSeconds = 4.0 
    override func viewDidLoad() { 
    super.viewDidLoad() 

    DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + delayInSeconds) { 

    segueToSecondVC() 

    } 

    } 

希望這有助於你解決這個問題....

+0

在這個例子中沒有必要爲segue方法實現準備。 –

+0

他想編程maly.look在我的答案底部我的筆記。 – Joe