0

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!

Placebo
  • 31
  • 6
  • Okay, how did you declare your properties? Is it like this: property (...) UIButton * closeButton; and then in your .m file synthesize closeButton = _closebutton? Or are the underscores everywhere? How is the IBOutlet called? – RDM Nov 23 '11 at 13:05

3 Answers3

0

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

Tendulkar
  • 5,550
  • 2
  • 27
  • 53
0

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!

RDM
  • 4,986
  • 4
  • 34
  • 43
  • ok, I have a Warning "!" in a bObj._closeButtonController = self; Incompatible pointer types assigning to "ClassA *" from "ClassB *" – Placebo Nov 23 '11 at 13:36
  • First off i made a slight typo, you should change the name of the variable in ClassB.h from closeButtonController to _closeButtonController. Secondly, in the code I provided where you assign the delegate of bObj to self, I assumed you're doing that inside a ClassA object. If you're not, change "self" to any ClassA object that holds the reference to the closeButton. – RDM Nov 23 '11 at 13:41
0

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;
    ...
}
  • i have an error in: '@protocol ButtonDelegate - (void)changeButtonFor:(ClassB *)class; @end;' Expected a type – Placebo Nov 23 '11 at 15:18
  • Put this before the `@protocol` statement: `@class ClassB;` . I have made some improvements: change the parameter name from `class` to `aClass` (both in the header and the implementation). – Luiz Carlos Querino Filho Nov 23 '11 at 15:26