Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 733
Roberto Viramontes Avatar asked Dec 28 '25 07:12

Roberto Viramontes


1 Answers

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

}
like image 71
Bista Avatar answered Dec 30 '25 22:12

Bista



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!