Joonwoo Kim Joonwoo Kim - 2 months ago 15
Swift Question

Change View Controller Image from another View Controller Swift 2.0

Can you please tell me how to change UIImageView's Image in ViewController A from View Controller B?

My Xcode version is Xcode 7, with Swift 2.0,

Thank you.

Answer

As thefredelement you can use NSNotification for that like in your ViewController B just add this code when you are dismissing view to ViewController A:

@IBAction func goBack(sender: AnyObject) {

    NSNotificationCenter.defaultCenter().postNotificationName("refresh", object: nil)
    self.dismissViewControllerAnimated(true, completion: nil)
}

And in your viewController A add this code in your viewDidLoad method:

override func viewDidLoad() {
    super.viewDidLoad()
    NSNotificationCenter.defaultCenter().addObserver(self, selector: "refreshList:", name:"refresh", object: nil)
}

And add this helper method in same class:

func refreshList(notification: NSNotification){

    println("parent method is called")
    //you can change your UIImageView's Image here.

}

Now when ever you press button from ViewController B it will call this method from ViewController A.

Note: I have tested this with Xcode 6.4 and I think it will work fine with Xcode 7 too.