简体   繁体   English

如何在不同视图中的某个视图中设置多个按钮的背景图像?

[英]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]; 这是我用来显示视图的代码:SettingsViewController * settingsView = [[SettingsViewController alloc] initWithNibName:nil bundle:nil]; [self presentModalViewController:settingsView animated:YES]; [自身presentModalViewController:settingsView动画:是];

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. 至于绑定对象集合并随后进行处理,我建议您查看IBOutletCollection关键字,该关键字允许将InterfaceBuilder的多个对象绑定到具有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. 您可以使用setBackgroundImage:forState:来设置图像。

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). 如果带有按钮的视图由其他视图控制(通过创建实例并使用addSubview:则可以使用instanceName.buttonName (只要将其声明为属性-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. 如果您不是手动添加视图,而是通过IB,则可以将控制另一个按钮的图像的按钮指向该类中的IBAction。

If neither of those options work you can always use NSNotificationCenter. 如果这些选项都不起作用,则可以始终使用NSNotificationCenter。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM