简体   繁体   中英

How do I rename a button title set from another view controller (Swift 3)

I'm trying to set a button title from ViewController1 by using the addTarget's completion function in ViewController2 and referencing a function in ViewController1 that would change the title of the button. The issue I'm having is that the button title doesn't change until ViewController1 is presented or a button on ViewController1 triggers the setButtonText() function. If someone could help explain and resolve the issue that would be awesome! Thanks in advance.

ViewController 1:

override func viewDidLoad() {
    super.viewDidLoad()
    buttonA.setTitle("FirstEdit", for: .normal)
    newViewController.addTarget(self, action: #selector(newVCHandler), for: .touchUpInside)
}

func newVCHandler(){
    present(ViewController2(), animated: true, completion: nil)
}

func setButtonText(){
    buttonA.setTitle("\(Title.buttonA)", for: .normal)
}

View Controller 2

override func viewDidLoad() {
    super.viewDidLoad()
    newTitleTextField.placeholder = "Set New Title"
    dismissButton.setTitle("Save and Dismiss", for: .normal)
    dismissButton.addTarget(self, action: #selector(dismissHandler), for: .touchUpInside)
}

func dismissHandler(){
    dismiss(animated: true, completion:{
    ViewController1().setButtonText()
})

Samah gave you the answer in their comment.

Any time you have code of the form ClassName() , you are invoking an initializer and creating a new instance of the class. This is like buying a new car, setting the station on the radio, discarding the new car, and wondering why the station on your old car's radio didn't change.

Your line ViewController1().setButtonText() is creating a new instance of ViewController that is not on-screen, and calling setButtonText on that new instance.

You need a reference to the other view controller (View Controller 1, as you call it) from View Controller 2. For us to help with that you'd have to explain how these view controllers are being created.

Try this :

    //ViewController 1:

    override func viewDidLoad() {
        super.viewDidLoad()
        buttonA.setTitle("FirstEdit", for: .normal)
        newViewController.addTarget(self, action: #selector(newVCHandler), for: .touchUpInside)
    }

    func newVCHandler(){
        let vc2 = ViewController2() //Create new instance of ViewController2 and save it in variable
        vc2.refvc1 = self //save ViewController1 reference in variable
        present(vc2, animated: true, completion: nil)
    }

    func setButtonText(){
        buttonA.setTitle("\(Title.buttonA)", for: .normal)
    }

    //View Controller 2

    var refvc1: ViewController1?
    override func viewDidLoad() {
        super.viewDidLoad()
        newTitleTextField.placeholder = "Set New Title"
        dismissButton.setTitle("Save and Dismiss", for: .normal)
        dismissButton.addTarget(self, action: #selector(dismissHandler), for: .touchUpInside)
    }

    func dismissHandler(){
        dismiss(animated: true, completion:{
            self.refvc1.setButtonText()
        })

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM