【发布时间】:2011-05-21 13:40:05
【问题描述】:
我正在尝试扩展 UIButton 类以添加一些方法,但是当我尝试初始化我的对象时遇到了一些问题。
-(id)init{
UIImage* image = [UIImage imageNamed:@"button_background.png"];
CGRect frame= CGRectMake(100.0, 70.0, 45.0 ,45.0);
self.frame = frame;
[self setTitle:@"title" forState:UIControlStateNormal];
[self setBackgroundImage:image forState:UIControlStateNormal];
[self setImage:image forState:UIControlStateNormal];
NSLog(@"type: %d",self.buttonType);
NSLog(@"x: %f\ny: %f\nwidth: %f\nheight: %f",frame.origin.x, frame.origin.y ,frame.size.width, frame.size.height);
NSLog(@"x: %f\ny: %f\nwidth: %f\nheight: %f",self.frame.origin.x, self.frame.origin.y ,self.frame.size.width, self.frame.size.height);
if(self != nil){
//
}
return self;
}
它可以正常工作,执行过程中没有警告或错误。 但是在控制台中会出现frame和self.frame的值有些不一致,当然按钮也不会出现在屏幕上。
type: 0
x: 100.000000
y: 70.000000
width: 45.000000
height: 45.000000
x: 0.000000
y: 0.000000
width: 0.000000
height: -1.998576
请帮帮我,我快疯了! :\
【问题讨论】:
-
另外,继承
UIControl而不是UIButton是最安全的。第一个用于子类化,第二个......
标签: ios cocoa-touch uibutton