2012-07-25 53 views
1

我已經對UIButton類進行了擴展。現在,如果我使用它,一切都很好。但我有另一個類爲我創建與UIButton對象數組,我在這裏有一些問題。UIButton類別問題

我有輔助類與方法,返回給我數組與UIButton對象。

在ViewController.m的viewDidLoad中的回調我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; 
} 

回答

2

您需要從您的類別中刪除initWithFrame:。通過包含它,您將刪除正常實現,並且這將阻止您的按鈕被正確創建,實際上您將返回UIControl

調用在一個類別super實現不呼叫UIButton的實現,但UIButton超 - UIControl。類別與子類不同。 (你也不應該將UIButton也歸類,因爲它是一個類集羣)。

如果您確實有專家初始化代碼,您必須將其添加到單獨的方法並在創建循環中調用它。

+0

是的,謝謝我做到了 – 2012-07-25 10:13:08

+0

'UIButton'不是類集羣。它的類方法'buttonWithType:'可能不會返回你的子類的一個實例,但'init'系列中的方法會。 – 2014-09-25 17:14:59