I am trying to create a sort of ‘settings’ page, and I am having a hard time switching the background image of my original view. The code, so far, is:
-(IBAction)switchBackground:(id)sender {
ViewController *mainView = [[ViewController alloc] initWithNibName:nil bundle:nil];
mainView.displayedImage.image = [UIImage imageNamed:@"image.png"];;
}
Perhaps I can get some pointers?
Thanks, all.
You are creating a new
mainViewobject each time you callswitchBackgroundmethod. You must change the background of an existing object to see the change happen.From your code it is hard to say where
switchBackgroundmethod is located.ViewController?If it is located in the view controller then all you have to do is:
EDIT
As per your comment.
When you want to change the image of an object of class A from class B you can do it in two different ways:
1. Through reference to an object
this is the settings initializer which on creation gets the pointer to your existing mainView
2. By posting local notification through NSNotificationCenter.
Now in your ViewController listen to the notification and react
in the init method in ViewController