0

I would like to extend UIButton with an NSString for some meta info. How could I do this?

I am starting with this:

@interface UIButton (neoUtils)

+ (UIButton *)neoName:(NSString *)buttonName;  

@end

and the .m

#import "UIButton+NAME.h"

@implementation UIButton (neoUtils)

+ (UIButton *)neoName:(NSString *)buttonName {
    UIButton *button = [UIButton neoName:buttonName];
    NSLog(@"%@",button);
    return button;
}

@end

Is this on the right path? And if so - how would I possibly use it?

malaki1974
  • 1,605
  • 3
  • 16
  • 32

1 Answers1

1

I am assuming that

@interface UIButton (neoUtils)

is declared in UIButton+NAME.h.

First of all, looks like that program will enter a recursive loop as soon as you call that method:

+ (UIButton *)neoName:(NSString *)buttonName {
    UIButton *button = [UIButton neoName:buttonName]; // <- this one
    NSLog(@"%@",button);
    return button;
}

because it will recurevely call the method itself.

Anyway, considering the extended object has to have a state (the NSString for the meta info has to be "remembered") I don't believe it's possible to fulfill the requirement with a category, that just extends the behaviour of the class. Then your solution didn't start on the right step I guess.

Instead I vould just create a class like

@interface XYMetaInfoButton : UIButton

@proerty (nonatomic, [strong|retain]) NSString *name;

@end

That you can then import globally in the project to have the new UIButton with meta-info. But that's just a possibility, maybe someone has a better solution.

Nicola Miotto
  • 3,647
  • 2
  • 29
  • 43