创builduibutton子类

我试图将子类UIButton包括一个活动指标,但是当我使用initWithFrame :(因为我是inheritance的uibutton我没有使用buttonWithType :)button不显示。 另外我怎么会在这种情况下设置buttontypes?

我的视图控制器:

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]; 

我的activityindicatorbutton类:

 #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类。 你想要做什么,需要一个子类?

喜欢inheritance的构成。

创build一个包含你需要的组件的UIView,并将它们添加到你的视图中。

我碰到类似的情况,并同意杰夫,你并不需要inheritanceUIButton。 我通过inheritanceUIControl来解决这个问题,然后重写layoutSubviews来完成我想要的“button”上所有视图的configuration。 这是一个更简单的实现,inheritanceUIButton,因为似乎有一些隐藏的mojo在引擎盖下。 我的实现看起来像这样:

 - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { self.opaque = YES; self.imageView = [[UIImageView alloc] initWithFrame:CGRectZero]; [self addSubview:self.imageView]; self.textLabel = [[UILabel alloc] initWithFrame:CGRectZero]; [self addSubview:self.textLabel]; } return self; } 

layoutSubviews是这样的:

 - (void)layoutSubviews { [super layoutSubviews]; // Get the size of the button CGRect bounds = self.bounds; // Configure the subviews of the "button" ... } 

我已经创build了一个自定义的类,宁愿构造而不是inheritance,它的工作原理是完美的。 我的自定义类有一个button,它知道它是MCContact对象。 此外,它绘制一个合适的button,并使用MCContact对象自动计算帧,通过。

头文件示例:

 #import <UIKit/UIKit.h> @protocol MCContactViewDelegate; @interface MCContactView : UIView { } @property (nonatomic, strong) MCContact *mcContact; @property (nonatomic, weak) id <MCContactViewDelegate> delegate; - (id)initWithContact:(MCContact*)mcContact delegate:(id <MCContactViewDelegate>)delegate; @end @protocol MCContactViewDelegate <NSObject> - (void)contactViewButtonClicked:(MCContactView*)contactView; @end 

实施文件:

 #import "MCContactView.h" @interface MCContactView() { UIButton *_button; } @end @implementation MCContactView - (id)initWithContact:(MCContact*)mcContact delegate:(id <MCContactViewDelegate>)delegate { self = [super initWithFrame:CGRectZero]; if (self) { GetTheme(); _mcContact = mcContact; _delegate = delegate; _button = [UIButton buttonWithType:UIButtonTypeCustom]; UIImage *normalBackgroundImage = [[UIImage imageNamed:@"tokenNormal.png"] stretchableImageWithLeftCapWidth:12.5 topCapHeight:12.5]; [_button setBackgroundImage:normalBackgroundImage forState:UIControlStateNormal]; UIImage *highlightedBackgroundImage = [[UIImage imageNamed:@"tokenHighlighted.png"] stretchableImageWithLeftCapWidth:12.5 topCapHeight:12.5]; [_button setBackgroundImage:highlightedBackgroundImage forState:UIControlStateHighlighted]; _button.titleLabel.font = [theme contactButtonFont]; [_button setTitleColor:[theme contactButtonTextColor] forState:UIControlStateNormal]; [_button setTitleEdgeInsets:UIEdgeInsetsMake(4, 6, 4, 6)]; NSString *tokenString = ([allTrim(mcContact.name) length]>0) ? mcContact.name : mcContact.eMail; [_button setTitle:tokenString forState:UIControlStateNormal]; [_button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside]; CGSize size = [tokenString sizeWithFont:[theme contactButtonFont]]; size.width += 20; if (size.width > 200) { size.width = 200; } size.height = normalBackgroundImage.size.height; [_button setFrame:CGRectMake(0, 0, size.width, size.height)]; self.frame = _button.frame; [self addSubview:_button]; } return self; } - (void)buttonClicked:(id)sender { [self.delegate contactViewButtonClicked:self]; } /* // Only override drawRect: if you perform custom drawing. // An empty implementation adversely affects performance during animation. - (void)drawRect:(CGRect)rect { // Drawing code } */ @end 

你有一个非常明显的问题,涉及你的dealloc方法:[super dealloc]; 必须在实现的最后被调用,否则之后的行会尝试访问已经释放的内存空间(伊娃空间),所以会崩溃。

对于另一个问题,我不确定把一个活动监视器作为一个button的子视图是一个好主意。