UIAlertAction actionsheet fetch data to childVC

40 views Asked by At

I have a parentVC that allows user to add by using actionsheet's choices

Expected Result :

when actionsheet presented, user choose the choices and the actionsheet will perform segue to childVC and childVC's label.text will become the chosen choice

My issue is I print out self.textInAS is exactly what the action.title, however, when segue performed, textFromAS become nil, so after some research I guess I lack of closure that required, however I am still new in Swift and I not sure how to properly perform a closure. Please provide example with code to help.

Many Thanks!!

/* parentVC */

    var textInAS : String?

 @IBAction func addBtnPressed(_ sender: UIBarButtonItem) {

        let alert = UIAlertController(title: "Alert Title", message: "alert msg", preferredStyle: .actionSheet)

   let actionA = UIAlertAction(title: "Choices A", style: .default) { (action) in
            let chosenTitle = action.title
            self.textInAS = choosenTitle
            self.performSegue(withIdentifier: "goChildVC", sender: self)

        }

  let actionB = UIAlertAction(title: "Choices B", style: .default) { (action) in
            let chosenTitle = action.title
            self.textInAS = choosenTitle
            self.performSegue(withIdentifier: "goChildVC", sender: self)

        }

            alert.addAction(actionA)
            alert.addAction(actionB)

            present(alert, animated: true, completion: nil)
        }




    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if (segue.identifier == "goChildVC") {
                let destinationVC = segue.destination as! ChildTableViewController
                destinationVC.textFromAS = self.textInAS
            }
        }
    }

}

/* ChildVC */

    @IBOutlet weak var label: UILabel!

    var textFromAS: String?

 override func viewDidLoad() {
        super.viewDidLoad()

        label.text = textFromAS
    }

0

There are 0 answers