Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/122.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/apache-flex/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios UIButton类别问题_Ios_Uibutton_Objective C Category - Fatal编程技术网

Ios UIButton类别问题

Ios UIButton类别问题,ios,uibutton,objective-c-category,Ios,Uibutton,Objective C Category,我已经为UIButton类做了扩展。现在如果我用它,一切都很好。但我有另一个类,它使用UIButton对象为我创建数组,这里我有一些问题 我有一个Helper类,其方法为我返回带有UIButton对象的数组 在ViewController.m的viewDidLoad回调中,我请求这个数组,在ViewController.m中,我导入我的UIButton+扩展名.m 所以,现在我有了在ViewController.m中使用的每个UIButton对象的扩展 但若我使用扩展名,那个么调用的方法就会出错

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

我有一个Helper类,其方法为我返回带有UIButton对象的数组

在ViewController.m的viewDidLoad回调中,我请求这个数组,在ViewController.m中,我导入我的UIButton+扩展名.m

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

但若我使用扩展名,那个么调用的方法就会出错

[thumbButton.titleLabel setFont:[UIFont fontWithName:@"Helvetica" size:11.0]];
但如果我不使用扩展,这个方法调用正确

这是我的UIButton+扩展名.h文件

#import <UIKit/UIKit.h>

@interface UIButton (extension_button)

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

@end
m(为我返回数组的方法,这里我对setFont方法有问题)

+(NSMutableArray*)CreateTumbNailsForCropSize{
CGFloat宽度=0.0f;
CGFloat启动位置=0.0f;
如果(是IPHONE){
宽度=320.0f;
启动位置=62.0f;
}
否则{
宽度=768.0f;
启动位置=286.0f;
}
NSMutableArray*arr=[[NSMutableArray alloc]init];
NSArray*分辨率阵列=[NSArray阵列,其对象为:@“30x40”、“33x48”、“35x40”、“35x45”、“36x47”、“37x47”,
@"40x50,"40x60,"43x55,"45x50,"50x50,"50x70,无";;
NSInteger pos_x=开始位置;
NSInteger page=0;
对于(NSInteger idx=0;idx<12;idx++){
如果(idx%3==0&&idx!=0){
page++;
pos_x=开始位置+宽度*页面;
}
UIButton*thumbButton=[[UIButton alloc]init];
[拇指按钮设置标签:idx];
[拇指按钮设置框:CGRectMake(位置x,13,60,60)];
[thumbButton setTitle:[NSString stringWithFormat:[resolutionArray objectAtIndex:idx]]用于状态:UIControlStateNormal];
[thumbButton.titleLabel setFont:[UIFont fontWithName:@“Helvetica”大小:11.0];
[拇指按钮设置标题:@“!00”用于状态:UIControlStateNormal];
[thumbButton setBackgroundImage:[UIImage ImageName:@“block_without_photo.png”]用于状态:UIControlStateNormal];
[arr addObject:thumbButton];
pos_x+=68;
}
返回arr;
}

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

在类别中调用
super
实现并不调用
UIButton
的实现,而是调用
UIButton
-
UIControl
的超类。类别与子类不同。(您也不应该将
UIButton
子类化,因为它是一个类集群)


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

ui按钮
不是类群集。它的class方法
按钮的类型:
可能不会返回子类的实例,但是
init
族中的方法会返回。
#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
+ (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;
}