1

UIButton クラスの拡張を行いました。今、私がそれを使用すると、すべてがうまく機能します。しかし、UIButton オブジェクトを使用して配列を作成する別のクラスがあり、ここでいくつかの問題があります。

UIButton オブジェクトを含む配列を返すメソッドを持つ Helper クラスがあります。

viewDidLoad コールバックの ViewController.m でこの配列を要求し、ここで ViewController.m にも UIButton+Extension.m をインポートします

これで、ViewController.m で使用する各 UIButton オブジェクトの拡張ができました。

しかし、拡張機能を使用すると、呼び出すメソッドでエラーが発生しました

[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;
}
4

1 に答える 1

2

カテゴリから削除する必要がありinitWithFrame:ます。これを含めることで、通常の実装を削除します。これにより、ボタンが適切に作成されなくなり、実際にはが返されますUIControl

superカテゴリ内の実装を呼び出すと、の実装は呼び出されませが、 -UIButtonのスーパークラスが呼び出されます。カテゴリはサブクラスとは異なります。(クラスクラスターであるため、サブクラス化しないでください)。UIButtonUIControlUIButton

そこにスペシャリストの初期化コードがある場合は、それを別のメソッドに追加して、作成ループ中に呼び出す必要があります。

于 2012-07-25T10:10:58.083 に答える