创建uibutton子类 [英] create uibutton subclass
问题描述
我试图将UIButton子类化为包含一个活动指示符,但是当我使用initWithFrame :(因为我是uibutton的子类,我不使用buttonWithType :)按钮不会显示。另外,在这种情况下我如何设置按钮类型?:
I tried to subclass UIButton to include an activity indicator, but when i use initWithFrame:(since i'm subclassing uibutton i'm not using buttonWithType:) the button doesn't display. Also how would i set the button type in this case?:
我的视图控制器:
ActivityIndicatorButton *button = [[ActivityIndicatorButton alloc] initWithFrame:CGRectMake(10, 10, 300, 44)];
[button addTarget:self action:@selector(buttonPressed) forControlEvents:UIControlEventTouchUpInside];
[button setTitle:@"Older Posts..." forState: UIControlStateNormal];
[cell addSubview:button];
[button release];
my activityindicatorbutton class:
my activityindicatorbutton class:
#import <Foundation/Foundation.h>
@interface ActivityIndicatorButton : UIButton {
UIActivityIndicatorView *_activityView;
}
-(void)startAnimating;
-(void)stopAnimating;
@end
@implementation ActivityIndicatorButton
- (id)initWithFrame:(CGRect)frame {
if (self=[super initWithFrame:frame]) {
_activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
_activityView.frame = CGRectOffset(_activityView.frame, 60.0f, 10.0f);
[self addSubview: _activityView];
}
return self;
}
-(void) dealloc{
[super dealloc];
[_activityView release];
_activityView = nil;
}
-(void)startAnimating {
[_activityView startAnimating];
}
-(void)stopAnimating {
[_activityView stopAnimating];
}
@end
推荐答案
你真的不想继承 UIButton
。它是一个类集群,因此单个实例将类似于 UIRoundRectButton
或其他一些私有Apple类。你想做什么需要一个子类?
You don’t really want to subclass UIButton
. It’s a class cluster, so individual instances will be something like UIRoundRectButton
or some other private Apple class. What are you trying to do that requires a subclass?
这篇关于创建uibutton子类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!