Search code examples
iosbuttonviewcontrolleruistoryboardsegue

How to make a button change another button's title on a different view controller


I'm trying to have a button send it's title to a new view controller, and the have a button on the new view controller have it's title set to the original button title.

I know how to do this on one view controller:

@IBAction func buttonPressed(_ sender: Any) {
        buttonOne.setTitle("Hello", for: .normal)
        buttonTwo.setTitle("Hi there", for: .normal)

But not on another...

I thought maybe to use

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    let sendTitle = segue.destination as! ViewControllerTwo
    sendTitle.notesent = sender as! String

but I get an error saying that the button can't be translated into a string, which makes sense to me, but I'm not sure how to target the string that is the button's title...

Thanks very much for any assistance!


Solution

  • From your question it appears that you just wondering how to get a title from the button.

    In your code you have to exchange sender as! String with

    (sender as! UIButton).title(for: .normal)