简体   繁体   中英

How to call a function after performing a segue in swift?

Hello I want to know what is the best way to call a func after performing a segue on swift. I want to click on a button and perform a segue and start my viewDidLoad() with a new 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: After finish first section of questions user can choose to finish the game or go to next level, next level is a new view controller so we performing this segue in a second view controller not in the same one.

ViewController1:

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

ViewController2:

Define a 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()
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM