Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
979 views
in Technique[技术] by (71.8m points)

ios - Change button color by pressing another button

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?

Thanks in advance !

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...