按另一个按钮更改按钮颜色

时间:2014-12-25 21:32:07

标签: ios objective-c xcode

首先,我很抱歉这个愚蠢的问题,我是超级iOS新手开发者......

可以通过按下另一个视图控制器中的另一个按钮来更改按钮的颜色并保存吗?

提前致谢!

2 个答案:

答案 0 :(得分:1)

您可以使用自定义委托,该委托用于将消息从一个对象发送到另一个对象。因此,当您按下另一个视图控制器的按钮时,只需在自定义委托方法中发送颜色。请参阅此Write Custom Delegates in this StackOverflow answer

请参阅以下示例代码以更改按钮的颜色: -

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 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 class

@interface AWindowController : NSWindowController< customDelegateColor> //conforming to the protocol

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 
    }

答案 1 :(得分:0)

是的!您只需要以某种方式将颜色应该更改的信息(以及可能的颜色应该更改为)传递给另一个视图控制器。

您可以在这里找到对可能的对象间通信方法的精彩介绍:http://nshipster.com/nsnotification-and-nsnotificationcenter/