pro*_*ock 6 iphone objective-c
我试图将UIButton子类化为包含一个活动指示符,但是当我使用initWithFrame :(因为我是uibutton的子类我没有使用buttonWithType :)按钮不显示.在这种情况下我如何设置按钮类型?:
我的视图控制器:
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];
Run Code Online (Sandbox Code Playgroud)
我的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
Run Code Online (Sandbox Code Playgroud)
我遇到了类似的情况,并同意杰夫的说法,你真的不需要继承UIButton.我通过继承UIControl来解决这个问题,然后重写layoutSubviews来完成我想要的"按钮"视图的所有配置.这是一个更简单的实现,它继承了UIButton,因为似乎有一些隐藏的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;
}
Run Code Online (Sandbox Code Playgroud)
layoutSubviews看起来像这样:
- (void)layoutSubviews {
[super layoutSubviews];
// Get the size of the button
CGRect bounds = self.bounds;
// Configure the subviews of the "button"
...
}
Run Code Online (Sandbox Code Playgroud)
Jef*_*ley -2
您确实不想\xe2\x80\x99t 子类化UIButton. 它\xe2\x80\x99是一个类簇,因此各个实例将类似于UIRoundRectButton或其他一些私有Apple类。您想要做什么需要子类?
| 归档时间: |
|
| 查看次数: |
16083 次 |
| 最近记录: |