Quote:
Originally Posted by learningtocode
I have been unsuccessful in the finding this answers. What is the best way to change a UIImageView's image that is on ViewController 1, from UIButtons from ViewController2?
|
There are several ways to do this. Here is one.
On viewcontroller 1, create an observer in viewDidLoad
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(methodToRun) name:@"RunThisMethod" object:nil];
Note: Be sure to add this to dealloc:
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"RunThisMethod" object:nil];
Add the method to change the image:
- (void)methodToRun {
self.someImageView.image = [UIImage imageNamed:@"New_Image.jpg"];
}
On view controller 2, add the postNotification call to the button action:
[button addTarget:self action:@selector(sendPost) forControlEvents:UIControlEventTouchUpInside];
Add the method sendPost:
- (void)sendPost {
[[NSNotificationCenter defaultCenter] postNotificationName:@"RunThisMethod" object:nil];
}
That's it. If you need the new image to be variable, change methodToRun to methodToRun

NSNotification *)notification and send a userInfo dictionary with the postNotification that has the image string.
Alternatively, you could store the image string in a singleton that is written by one, and read by the other. A notification would not be necessary if you changed the image in viewWillAppear of view controller 1 based upon the image string stored in the singleton.
Hope that helps.