0

In an technical phone interview, interviewer asked me to implement MiniButton class like follow and asked me to implement some methods that will do what UIButton method does.

@interface MiniButton

-(void)addTarget:(id)target action:(SEL)action;
-(void)removeTarget:(id)target action:(SEL)action;
-(void)_callAllTargets;

@end

Above is the only information given to me. I was told that I can not subclass MiniButton from UIButton. Besides I can assume whatever local/private variables if I need any.

How can we implement those methods ?

9
  • Identifiers that start with an underscore and a capital letter are reserved for use by the implementation. Commented Feb 11, 2013 at 23:07
  • @CarlNorum: As C identifiers, sure. I don't think that applies to obj-c method names though. Commented Feb 11, 2013 at 23:10
  • @KevnBallard, it has to - Objective-C is a strict superset of C. What if the implementation had #define _CallAllTargets someplace? Commented Feb 11, 2013 at 23:10
  • I don't remember exactly whether it was _Call... or _call... Commented Feb 11, 2013 at 23:13
  • Apple reserves method names beginning with an underscore: Private Methods. But that's not really relevant to this question, since the method names were given to the asker. Commented Feb 11, 2013 at 23:15

2 Answers 2

2

Assuming you are not allowed to make a subclass of UIControl, you need to choose a data structure for storing the target/action pairs. Since a button doesn't normally retain its targets, you can just define a struct like this:

typedef struct {
    __unsafe_unretained id target;
    SEL action;
} TargetAction;

You can store these in an NSMutableArray by wrapping them in instances of NSValue.

The easiest way to invoke an action is using the performSelector:withObject:, like this:

TargetAction ta;
[valueWrapper getValue:&ta];
[ta.target performSelector:ta.action withObject:self];
Sign up to request clarification or add additional context in comments.

Comments

0

I guess the goal was to make a short cut class of UIControl with factoring away the most common touch event UIControlEventTouchUpInside:

@interface MiniButton : UIControl

-(void)addTarget:(id)target action:(SEL)action;
-(void)removeTarget:(id)target action:(SEL)action;
-(void)_callAllTargets;

@end

@implementation MiniButton
-(void)addTarget:(id)target action:(SEL)action
{
    [self addTarget:target action:action forControlEvents: UIControlEventTouchUpInside];
}

-(void)removeTarget:(id)target action:(SEL)action{
    [self removeTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
}

-(void) _callAllTargets
{
     [self sendActionsForControlEvents:UIControlEventTouchUpInside];
}
@end

Another option could have been, that he wanted you to extend UIButton. But as UIButton is a so-called class cluster (comparable with a factory), it should not be extended by sub-classing but can be extended by creating a category on UIControl, the parent class of UIButton. Now any instance of any button is extended, no matter what subclass is returned.


I think he wanted you to show some knowledge about the fact, that actually UIButton and it real classes are just a small layer on top of UIControl. UIButton has only the reposibility of showing the button's label, image,… . all other things reside in UIControl and it ancestors.

4 Comments

I was told that I can not override the methods.
it did not overwrite any methods. The methods you posted do not exists on UIControl.
and addTarget:action:forConrolEvents: and all other I call are UIControl methods, not UIButton.
I think he wanted you to show some knowledge about the fact, that actually UIButton and it subclasses are just a small layer on top of UIControl. UIButton has only the reposibility of showing the button's label, image,… . all other things reside in UIControl and it ancestors.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.