简体   繁体   中英

Delegate Method Swift not working correctly

I'm trying to get my view controllers to communicate with each other and pass values that can be changed to each other. In this particular case I'm trying to pass a value from the 1st controller to the 2nd controller, change the value in the 2nd controller, then pass the value back from the 2nd controller to the first controller. I can do steps 1 and 2 however the 3rd step isn't working, when i try to get the changed value it isn't changed when it gets to viewDidLoad(). Here is example code:

//first controller
class OpenViewController: UIViewController, UpdateDelegate {
    var text: String!

    func save(controller: ViewController, text2: String) {
            text = text2//set the new value of text to text2
            controller.navigationController?.popViewControllerAnimated(true)
            println(text)
        }


    override func viewDidLoad() {
        super.viewDidLoad()
        if(text.isEmpty) { //if text is empty set value to hi
            text = "hi"
        }
        else{
            print(text)
        }
    }

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
            if segue.identifier == "ToList" {
                var vc = segue.destinationViewController as! ViewController
                vc.text2 = text
                vc.delegate = self

            }
        }

    }
}

//second controller
protocol UpdateDelegate{
    func save(controller: ViewController, text2: String)
}

class ViewController: UIViewController {
    var delegate: UpdateDelegate? = nil
    var text2: String!
    @IBAction func home(sender: AnyObject) { //button click to go back to first view controller
        delegate!.save(self, text2: String) //save the updated value into first view controller
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        self.text2 = text! //set value of text2 to the  value of text from previous view controller
        self.text2 = "bye"//change text2 value to "bye"
    }
}

The print statements are there to check if it is working as intended. The println(text) in the save function actually shows that text has a changed value of text2, however when it gets down to viewDidLoad() in OpenViewController text is now empty again. I was under the impression of with delegate that the value of text in OpenViewController would be text2 from ViewController.

In general you probably don't want to think about having ViewControllers talk directly to each other.

What you want is a third entity that is long lived and that both ViewControllers can talk to. Let's call this TextManager for now.

So OpenViewController makes a call to TextManager to set the text that other UIViewControllers may need. And then your second UIViewController can also talk to TextManager to retrieve the text. Likewise it can send changes to TextManager that the first viewController can read.

UIViewControllers don't live for very long - they come and go. So you need a third thing to handle your long lived data.

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