按另一个按钮更改按钮颜色 [英] Change button color by pressing another button

查看:39
本文介绍了按另一个按钮更改按钮颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先我很抱歉这个愚蠢的问题,我是超级超级 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 
    }

这篇关于按另一个按钮更改按钮颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆