如何在swift中执行segue后调用函数?

时间:2016-09-18 06:44:39

标签: ios swift function segue

您好我想知道在swift上执行func后调用segue的最佳方法是什么。我想点击一个按钮并执行segue并使用新的viewDidLoad()启动我的func

//FIRST VIEW CONTROLLER

import UIKit

class CelebritiesViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()

    questionsLevel1()
    pickingRandomQuestion()
    hide()
    finishGame.hidden = true
    nextLevel.hidden = true

    if isButtonClick == true {
        questionsLevel2()
    }

}
}
//SECOND VIEW CONTROLLER


import UIKit

class Level2Section: UIViewController {

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "level2" {
        if let destinationLevel2 = segue.destinationViewController as? CelebritiesViewController {
            destinationLevel2.isButtonClick = true
        }
    }
}
}
PS:完成第一部分问题后,用户可以选择完成游戏或进入下一级别,下一级是一个新的视图控制器,所以我们在第二个视图控制器中执行此segue不在同一个视图中。

1 个答案:

答案 0 :(得分:4)

<强> ViewController1:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "SegueIdentifierHere" {
        if let destVC = segue.destinationViewController as? ViewController2 {
            destVC.isButtonClick = true
        }
    }
}

<强> ViewController2:

定义一个Bool:

var isButtonClick:Bool!

viewDidLoad中():

override func viewDidLoad() {
    super.viewDidLoad()

    questionsLevel1()//This is my first section of questions
    pickingRandomQuestion()//My function to make a random number of questions

    if isButtonClick == true {
        isButtonClick = !isButtonClick
        callSecondFunction()
    }

}