iOS 编程:扩展 UIButton 类时出现问题

发布于 2024-11-09 01:19:39 字数 1001 浏览 0 评论 0原文

我正在尝试扩展 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

邮友 2024-11-16 01:19:39

当您在 Objective-C 中进行子类化时,您必须 (1) 重写指定的初始值设定项 (2) 调用超类的指定初始值设定项。你两者都不做。

你应该做这样的事情:

/** 
 * \brief Convenience class method to replace button method of UIButton
 */
+(id)myButton
{
    MyButton* myButton = [[MyButton alloc] 
                              initWithFrame:CGMakeRect(100.0, 70.0, 45.0, 45.0)];
    return [myButton autorelaase];
}

-(id)initWithFrame:(CGRect)f
{
    if(self = [super initWithFrame:f])
    {
        UIImage* image = [UIImage imageNamed:@"button_background.png"];    

        [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", NSStringFromCGRect(frame));
        NSLog(@"x: %f\ny: %f\nwidth: %f\nheight: %f", NSStringFromCGRect(self.frame));
    }
    return self;
}

希望这有帮助。如果您需要更多信息,请告诉我。

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:

/** 
 * \brief Convenience class method to replace button method of UIButton
 */
+(id)myButton
{
    MyButton* myButton = [[MyButton alloc] 
                              initWithFrame:CGMakeRect(100.0, 70.0, 45.0, 45.0)];
    return [myButton autorelaase];
}

-(id)initWithFrame:(CGRect)f
{
    if(self = [super initWithFrame:f])
    {
        UIImage* image = [UIImage imageNamed:@"button_background.png"];    

        [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", NSStringFromCGRect(frame));
        NSLog(@"x: %f\ny: %f\nwidth: %f\nheight: %f", NSStringFromCGRect(self.frame));
    }
    return self;
}

Hope this helps. Let me know if you need more info.

烟花肆意 2024-11-16 01:19:39

在您登录时,您不会将按钮添加到任何视图中。
不管怎样,代码一定是这样的?

-(id)init{



if(self != nil){
    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);
}
return self;

顺便说一句,你不会忘记调用超类 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?

-(id)init{



if(self != nil){
    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);
}
return self;

By the way don't you forget call the superclass init ?

那一片橙海, 2024-11-16 01:19:39

我刚刚在这里回答了一个非常相似的问题:
如何创建非常自定义的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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文