Reputation: 35
I'm making a custom button in Objective-C, GLButton. I want to follow the Target-Action Design Pattern. So in GLButton I have
SEL _action;
NSObject *_target;
- (void) setAction:(SEL) action{
_action = action;
}
- (void) setTarget:(NSObject*) target{
_target = target;
}
and to perform the action it calls
[_target performSelector:_action];
In the class that instantiates GLButton I have the methods
- (void) button1{
NSLog(@"button1");
}
- (void) button2{
NSLog(@"button2");
}
I then set the respective target/action
[b1 setTarget:self];
[b1 setAction:@selector(button1)];
[b2 setTarget:self];
[b2 setAction:@selector(button2)];
but when I click either button it always fires the last target set, in this case button2.
Any idea how to fix this?
Upvotes: 1
Views: 990
Reputation: 162712
Are these instance variables or declared at a random place in your .m files?
SEL _action;
NSObject *_target;
They need to be instance variables or, better yet, @property declarations.
Upvotes: 2