【问题标题】:UIButton category issuesUIButton 类别问题
【发布时间】:2012-07-25 18:10:58
【问题描述】:

我为 UIButton 类做了扩展。现在,如果我使用它,一切都会很好。但是我有另一个类为我创建带有 UIButton 对象的数组,这里我遇到了一些问题。

我有一个带有 UIButton 对象的 array 方法的 Helper 类。

在 viewDidLoad 回调中的 ViewController.m 中,我请求这个 array 以及在 ViewController.m 中我导入我的 UIButton+Extension.m

所以,现在我有每个 UIButton 对象的扩展,我将在 ViewController.m 中使用

但是如果我使用扩展名,我会在调用方法上遇到错误

[thumbButton.titleLabel setFont:[UIFont fontWithName:@"Helvetica" size:11.0]];

但如果我不使用扩展,此方法调用正确。

这是我的 UIButton+Extension.h 文件

#import <UIKit/UIKit.h>

@interface UIButton (extension_button)

- (void)centerButtonToView:(UIView *)view;
- (UIImage *)cropeImage:(UIImageView *)imageView;

@end

这是我的 UIButton+Extension.m 文件

#import "UIButton+Extension.h"

@implementation UIButton (extension_button)

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

- (void)centerButtonToView:(UIView *)view {
    CGRect rect = view.frame;
    CGRect rectSelf = self.frame;
    rectSelf.origin.x = (rect.size.width / 2.0f - rectSelf.size.width / 2.0f) + rect.origin.x;
    rectSelf.origin.y = (rect.size.height / 2.0f - rectSelf.size.height / 2.0f) + rect.origin.y;
    self.frame = rectSelf;
}

- (UIImage *)cropeImage:(UIImageView *)imageView {    
    CGRect rect;
    rect.origin.x = self.frame.origin.x - imageView.frame.origin.x;
    rect.origin.y = self.frame.origin.y - imageView.frame.origin.y;
    rect.size.width = self.frame.size.width;
    rect.size.height = self.frame.size.height;    
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();    
    CGRect drawRect = CGRectMake(-rect.origin.x,-rect.origin.y, imageView.image.size.width, imageView.image.size.height);
    CGContextClipToRect(context, CGRectMake(0, 0, rect.size.width, rect.size.height));
    [imageView.image drawInRect:drawRect];
    UIImage* subImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return subImage; 
}

@end

Helper.m(为我返回数组的方法,这里我的 setFont 方法有问题)

+ (NSMutableArray *)createThumbnailsForCropSize {

    CGFloat width = 0.0f;
    CGFloat start_pos = 0.0f;

    if (IS_IPHONE) {
        width = 320.0f;
        start_pos = 62.0f;
    }
    else {
        width = 768.0f;
        start_pos = 286.0f;
    }

    NSMutableArray *arr = [[NSMutableArray alloc] init];
    NSArray *resolutionArray = [NSArray arrayWithObjects:@"30x40",@"33x48",@"35x40",@"35x45",@"36x47",@"37x47",
                                @"40x50",@"40x60",@"43x55",@"45x50",@"50x50",@"50x70", nil];
    NSInteger pos_x = start_pos;
    NSInteger page = 0;
    for (NSInteger idx = 0; idx < 12; idx++) {
        if (idx%3 == 0 && idx != 0) {
            page++;
            pos_x = start_pos + width * page;
        }
        UIButton *thumbButton = [[UIButton alloc] init];
        [thumbButton setTag:idx];
        [thumbButton setFrame:CGRectMake(pos_x, 13, 60, 60)];
        [thumbButton setTitle:[NSString stringWithFormat:[resolutionArray objectAtIndex:idx]] forState:UIControlStateNormal];
        [thumbButton.titleLabel setFont:[UIFont fontWithName:@"Helvetica" size:11.0]];
        [thumbButton setTitle:@"!00" forState:UIControlStateNormal];
        [thumbButton setBackgroundImage:[UIImage imageNamed:@"block_without_photo.png"] forState:UIControlStateNormal];
        [arr addObject:thumbButton];
        pos_x += 68;
    }
    return arr;
}

【问题讨论】:

    标签: ios uibutton objective-c-category


    【解决方案1】:

    您需要从您的类别中删除 initWithFrame:。通过包含它,您将删除正常的实现,这将阻止您的按钮被正确创建,您实际上将返回一个UIControl

    在类别中调用super 实现确实不是调用UIButton 的实现,而是UIButton 的超类-UIControl。类别不同于子类。 (你也不应该继承UIButton,因为它是一个类集群)。

    如果您确实有专门的初始化代码,则必须将其添加到单独的方法中并在创建循环期间调用它。

    【讨论】:

    • UIButton 不是类集群。它的类方法buttonWithType: 可能不会返回您的子类的实例,但init 系列中的方法会。
    猜你喜欢
    • 1970-01-01
    • 2011-05-21
    • 2012-07-23
    • 1970-01-01
    • 1970-01-01
    • 2021-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多