按另一个按钮更改按钮颜色
首先我很抱歉这个愚蠢的问题,我是超级超级 iOS 菜鸟开发者...
First i am sorry for the dumb question, i am super mega iOS rookie developer...
是否可以通过按下另一个视图控制器中的另一个按钮来更改按钮的颜色并保存?
It is possible to change the color of a button by pressing another button that it's in another view controller and save it?
提前致谢!
您可以使用自定义委托,用于将消息从一个对象发送到另一个对象.因此,当您按下另一个视图控制器的按钮时,只需在自定义委托的方法中发送颜色.请参阅此在此 StackOverflow 答案中编写自定义委托
You can use custom delegates, which is used for sending messages from one object to another. So when you pressed the button of another view controller then just send the color in the method of custom delegates. Refer this Write Custom Delegates in this StackOverflow answer
参考下面的示例代码来改变按钮的颜色:-
Refer below sample code to change the color of button:-
secondVwController.h 类
secondVwController.h class
@protocol customDelegateColor <NSObject>
@required
-(void)getColor:(UIColor*)color;
@end
@interface BWindowController : NSWindowController
{
id<customDelegateColor>delegate;
}
@property(nonatomic,assign)id<customDelegateColor>delegate;
@end
secondVwController.m 类
secondVwController.m class
- (IBAction)buttonPressed:(id)sender
{
//below only calling the method but it is impelmented in AwindowController class
if([[self delegate]respondsToSelector:@selector(getDataValue)]){
[[self delegate]getColor:[UIColor redColor]];
}
}
firstVwController.h 类
firstVwController.h class
@interface AWindowController : NSWindowController< customDelegateColor> //conforming to the protocol
firstVwController.m 类
firstVwController.m class
//Implementing the protocol method
-(void)getColor:(UIColor*)color
{
self.btn.color=color;
}
//In this method setting delegate AWindowController to BWindowController
-(void)someMethod{
BWindowController *b=[[BWindowController alloc]init];
}
-(IBAction)buttonPressed:(id)sender
{
b.delegate=self; //here setting the delegate
}