1

我的 .h 文件

#import <UIKit/UIKit.h>

@interface NavigationBarButton : UIButton

@property(nonatomic, retain) UIImageView *barButtonImage;

@property(nonatomic, retain) UILabel *barButtonLabel;

- (id)initWithFrame:(CGRect)frame withImage:(UIImage *) image withTitle:(NSString *) title;

我的 .m 文件

- (id)initWithFrame:(CGRect)frame withImage:(UIImage *) image withTitle:(NSString *) title


{

self = [super initWithFrame:frame];

if (self) 

{  
        self = [UIButton buttonWithType:UIButtonTypeCustom];

        self.frame = frame;
        self.backgroundColor = [UIColor clearColor];

        barButtonImage = [[[UIImageView alloc] initWithFrame:CGRectMake(16, 1, 18, 23)]autorelease];
        [barButtonImage setImage:image];

        barButtonLabel = [[[UILabel alloc] initWithFrame:CGRectMake(0, 26, 50, 10)]autorelease];
        barButtonLabel.backgroundColor = [UIColor clearColor];
        barButtonLabel.text = title;
        barButtonLabel.font = [UIFont systemFontOfSize:11];
        barButtonLabel.textAlignment = NSTextAlignmentCenter;

        [self addSubview:barButtonLabel];
        [self addSubview:barButtonImage];

        //[barButtonImage release];
        //[barButtonLabel release];
    }
    return self;
}

但应用程序在 [self addSubview:barButtonLabel] 上崩溃,这很奇怪,因为代码在 iPad 6.0 模拟器上运行良好,但在 iPad 5.0 或 iPad 5.1 模拟器上运行良好

4

1 回答 1

0

一旦检查这个

在 .m 文件中添加这个

@synthesize barButtonImage, barButtonLabel;
于 2013-04-02T13:36:39.647 回答