0

Hello I am trying to pass back a variable that has the level number. After the user clears the round below is called

        if enemyHP <= 0.0{
            level = level + 1                
            battle(winPlayer:true)
        }

then function battle is called

        let alert = UIAlertController(title: "finished!", message: finishedMessage, preferredStyle: UIAlertControllerStyle.alert)
        alert.addAction(UIAlertAction(title:"Go Back",style:.default,handler:{action in


        self.dismiss(animated: true, completion: nil)}))
        self.present(alert,animated: true,completion: nil)
    }

I am trying to display the level number in the previous view controller so I need the variable level to go be passed back. What is the best way for this? I have used segues and it doesn't work with the alert.

Delegate

protocol DataSentDelegate {

    func Back(data: Int)
}

I used

delegate?.Back(data: level)

In the previous ViewController I added

func Back(data: Int){
   new = data
}


print(new)

It doesn't appear.

Satsuki
  • 2,166
  • 5
  • 17
  • 33
  • Your code will not work. You are dismissing then you are presenting??? – Brandon Sep 15 '17 at 20:32
  • @Brandon Hello it does work. It shows the alert and it brings me back. I am having trouble passing the variable back – Satsuki Sep 15 '17 at 20:34
  • 2
    See https://stackoverflow.com/questions/5210535/passing-data-between-view-controllers – rmaddy Sep 15 '17 at 20:56
  • @Satsuki Enjoy your race condition. Your code doesn’t work. You are calling dismiss on self (asynchronously). Then you are calling present alert on self (also async). Tell me how that will work. Problem: dismissing self then presenting an alert on it will give you “cannot present alert on detached viewcontroller” warning.. – Brandon Sep 15 '17 at 23:01

1 Answers1

0

Instead of using the dismiss-present methods, you should use a segue and then pass the object in prepare(for:sender:) this way:

func prepare(for segue:UIStoryboardSegue, sender:Any?) 
{
    if segue.identifier == "Back" // Give it any name
    {
        if let destination = segue.destination as? MyViewController // NOTE: this is your custom view controller class
        {
            destination.level = yourDesiredValue
        }
    }
}

Update: to perform the segue:

let alert = UIAlertController(title: "finished!", message: finishedMessage, preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title:"Go Back",style:.default,handler:{action in
    self.performSegue(withIdentifier: "Back", sender: nil)
}
self.present(alert,animated: true,completion: nil)
Ramy Al Zuhouri
  • 21,580
  • 26
  • 105
  • 187
  • Hello Thank you for your answer, I know about the segues but will this work with the alert? – Satsuki Sep 15 '17 at 20:55
  • @Satsuki I'm not sure what you are trying to do, and I don't understand why you try to present the alert inside the same alert's completion callback. I suppose that you want to present the alert outside the completion block, and you want to perform the segue when the user presses the "Go Back" action of the alert. I edited the answer, I hope it helps. – Ramy Al Zuhouri Sep 15 '17 at 21:06