, , . Apple , , , , , - , , .
, , , UIButton, , , . UIButton - [UIButton buttonWithType:]. , , ():
@implementation MyCustomButton
+ (id)buttonWithType:(UIButtonType)buttonType {
return [super buttonWithType:buttonType];
}
@end
, , [MyCustomButton buttonWithType:], - UIButton, MyCustomButton. Apple UIButton init, UIButton.
- , UIView, , UIButton .
- :
@interface MyButton : UIView {}
- (void)buttonTapped;
@end
@implementation MyButton
-(id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = self.bounds;
[button addTarget:self action:@selector(buttonTapped)
forControlEvents:UIControlEventTouchUpInside];
[self addSubview:button];
}
return self;
}
- (void)buttonTapped {
}
@end
If you want the button to perform different actions depending on more complex user interactions, you can make more calls [UIButton addTarget:action:forControlEvents:]for different control events.
Link: Apple UIButton class reference
Tyler source
share