Change alpha and enabled UIButton of ClassA from C

2019-06-13 17:17发布

I change a UIButton's parameter in ClassB, but UIButton is declared in the ClassA. How do I do see from ClassB?

Simply:

_closeButton.alpha = 1;
_closeButton.enabled = TRUE;

Thanks for reply!!

ok I have tried this: in ClassB.h

ClassA *_closeButtonController;

in ClassB.m didLoad:

_closeButtonController = [[ClassA alloc] initWithNibName:@"ClassA" bundle:nil]; _closeButtonController._closeButton.enabled = TRUE; _closeButtonController._closeButton.alpha = 1;

I haven't error but dont' work!


@Warkst in ClassA.h I have delcared @property (nonatomic, retain) IBOutlet UIButton *_closeButton; and in ClassA.m @synthesize _closeButton; Use the underscore everywhere !

I have resolved with this code: in ClassA.m

    - (void)viewDidLoad
{
    [super viewDidLoad];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showAndEnableCloseButton) name:@"EnableCloseButton" object:nil];
}

....

- (void)showAndEnableCloseButton
{
    _closeButton.alpha = 1;
    _closeButton.enabled = YES;
}

In ClassB.m When "TouchUpInside" UIButton, close the View of ClassB and return on view of ClassA and call means NSNotificationCenter the changes in _closeButton of ClassA:

- (IBAction)close:(id)sender
{
    [[NSNotificationCenter defaultCenter]postNotificationName:@"EnableCloseButton" object:self];
    [self.view removeFromSuperview];
}

I hope I was of help to someone, Thank you all for your responses! Good Day!

3条回答
姐就是有狂的资本
2楼-- · 2019-06-13 17:43

You should do this using delegation. ClassA would be the delegate of ClassB. When ClassB needs to change the UIButton in ClassA, it calls a method from ClassA (its delegate) that does this directly. Inside ClassB:

[self.delegate changeButtonFor:self];

The changeButton method in ClassA would then do what you want, directly to the UIButton.

You need to create a delegate property in ClassB, and set it to ClassA before:

self.delegate = _closeButtonController;

This is what you need to put inside your ClassB header file:

@class ClassB;

@protocol ButtonDelegate
- (void)changeButtonFor:(ClassB *)aClass;
@end

@interface ClassB : ...
{
    id <ButtonDelegate> delegate;
}

@property (assign) id <ButtonDelegate> delegate;

Don't forget to synthesize it in the implementation file:

@synthesize delegate;

Now, inside the header file of ClassA, declare that it adopts the ButtonDelegate protocol:

#import "ClassB.h"

@interface ClassA: ... <ButtonDelegate>

Then implement the changeButtonFor method in the implementation to do what you want with the butted:

@implementation ClassA

- (void)changeButtonFor:(ClassB *)aClass
{
    button1.alpha = 0.5;
    ...
}
查看更多
聊天终结者
3楼-- · 2019-06-13 17:48

take the classA object in Class B.When you are going from class A to class B then set the classBObject.classAobj = self.

查看更多
戒情不戒烟
4楼-- · 2019-06-13 17:50

I suppose you could add a method in ClassA that sets the parameters of the button and then call that method from ClassB, assuming you have a reference to your ClassA object. So:

in ClassA.h

- (void)showAndEnableCloseButton;

in ClassA.m

- (void)showAndEnableCloseButton{
    self._closeButton.alpha = 1;
    self._closeButton.enabled = YES;
}

in ClassB.h

ClassA * _closeButtonController;
// ...
@property (nonatomic, retain) ClassA * _closeButtonController;

in ClassB.m

@synthesize _closeButtonController;

Then, in your ClassA object, where you make a ClassB object:

// somewhere in the code from ClassA.m
ClassB * bObj = [[ClassB alloc] init];
bObj._closeButtonController = self;
// ...

Then, when you want to modify the button from somewhere in ClassB, you simply do this:

//...
[self._closeButtonController showAndEnableCloseButton];
//...

This should work. Hope it helps!

查看更多
登录 后发表回答