简体   繁体   中英

How can I set the background image of multiple buttons in a certain view from a different view?

Basically, I cannot figure out how to change the background image. I have searched and searched and just cannot seem to find it. Anyone have any ideas? Thanks!

Update

Here is the code I use to show the View: SettingsViewController *settingsView = [[SettingsViewController alloc] initWithNibName:nil bundle:nil]; [self presentModalViewController:settingsView animated:YES];

If anyone needs anything else to help out I'll do my best! Thanks!

Passing a messages between different views can be done by direct call of the methods (not a good architectural solution but maybe not critical for a small projects) or with an event driven model as described in an answer above. As for binding of a collection of objects and processing the afterward I recommend to review IBOutletCollection keyword that allows to bind multiple objects fro an InterfaceBuilder to a property with a type like NSArray.

Property declaration will look like following:

@property (nonatomic, retain) IBOutletCollection(UIButton) NSArray *buttons;

The code to change the background for all the buttons will look like following:

UIImage *backgroundImage = [UIImage imageNamed:@"background.png"];
for(UIButton *button in buttons) {
    [button setBackgroundImage:backgroundImage forState:UIControlStateNormal];
}

You use setBackgroundImage:forState: to set the image.

Regarding the different views part, it depends on how you wrote your code. If the view with the buttons is controlled by the different view (by creating an instance and using addSubview: ) then you can call it directly by using instanceName.buttonName (as long as you declare it as a property -thanks fichek).

If you don't manually add the view, instead through IB, you can have the button that controls the other button's image point to the IBAction in that class.

If neither of those options work you can always use NSNotificationCenter.

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