iOS 编程:扩展 UIButton 类时出现问题
我正在尝试扩展 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
请帮助我,我快疯了! :\
I'm trying to extend UIButton class to add few methods but I'm having some problem when I try to init my object.
-(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;
}
It seams to work fine, there are no warning or error during execution.
But in the console appears some inconsistency between values of frame and self.frame, and of course the button does not appear on screen.
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
Please help me I'm getting out of my mind! :\
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当您在 Objective-C 中进行子类化时,您必须 (1) 重写指定的初始值设定项 (2) 调用超类的指定初始值设定项。你两者都不做。
你应该做这样的事情:
希望这有帮助。如果您需要更多信息,请告诉我。
When you are subclassing in Objective-C you must (1) override the designated initializer (2) call the designated initializer of the super-class. You do neither.
You should be doing something like this:
Hope this helps. Let me know if you need more info.
在您登录时,您不会将按钮添加到任何视图中。
不管怎样,代码一定是这样的?
顺便说一句,你不会忘记调用超类 init 吗?
In that moment when you log you don't added the button to any view.
Anyway the code must be look something like this?
By the way don't you forget call the superclass init ?
我刚刚在这里回答了一个非常相似的问题:
如何创建非常自定义的uibutton 就像 iOS 中的 UITableViewCellStyleSubtitle
就像其他人指出的那样,UIButton 是一个类簇,所以你可能不想子类化 它。但您可能会发现创建 UIButton 类别更容易,而不是子类化 UIControl。
I just answered a very similar question here:
How to create a very custom uibutton like UITableViewCellStyleSubtitle in iOS
Like some other folks have pointed out, UIButton is a class cluster, so you probably don't want to subclass it. But instead of subclassing UIControl, you might find it easier to create a category of UIButton.