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()
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With