我有一個創建的類來生成UIButton,我將它添加到我的UIView中。這個偉大的工作,直到我轉換到昨天的弧線,不是我得到以下錯誤:在iOS 5中動態生成UIButton ARC導致釋放實例崩潰
-[OrderTypeButton performSelector:withObject:withObject:]: message sent to deallocated instance 0x12449f70
這裏是將按鈕添加到我UIView代碼(實際上是我的主要的UIView子視圖):
OrderTypeButton *btn = [[OrderTypeButton alloc]initWithOrderType:@"All Orders" withOrderCount:[NSString stringWithFormat:@"%i",[self.ordersPlacedList count]] hasOpenOrder:NO];
btn.view.tag = 6969;
btn.delegate = self;
[btn.view setFrame:CGRectMake((col * width)+ colspacer, rowHeight + (row * height), frameWidth, frameHeight)];
[self.statsView addSubview:btn.view];
這裏是我的類的頭:
#import <UIKit/UIKit.h>
@protocol OrderTypeButtonDelegate
-(void) tapped:(id)sender withOrderType:(NSString*) orderType;
@end
@interface OrderTypeButton : UIViewController {
id<OrderTypeButtonDelegate> __unsafe_unretained delegate;
IBOutlet UILabel *lblOrderType;
IBOutlet UILabel *lblOrderCount;
NSString *orderType;
NSString *orderCount;
BOOL hasOpenOrder;
}
@property (nonatomic, strong) IBOutlet UIButton *orderButton;
@property (nonatomic, strong) IBOutlet UILabel *lblOrderType;
@property (nonatomic, strong) IBOutlet UILabel *lblOrderCount;
@property (nonatomic, strong) NSString *orderType;
@property (nonatomic, strong) NSString *orderCount;
@property (nonatomic, assign) BOOL hasOpenOrder;
@property (nonatomic, unsafe_unretained) id<OrderTypeButtonDelegate> delegate;
-(id) initWithOrderType: (NSString *) anOrderType withOrderCount: (NSString *) anOrderCount hasOpenOrder: (BOOL) openOrder;
-(IBAction)btnTapped:(id)sender;
@end
實現:
#import "OrderTypeButton.h"
@implementation OrderTypeButton
@synthesize orderButton;
@synthesize lblOrderType, lblOrderCount, orderType, orderCount, hasOpenOrder, delegate;
-(id) initWithOrderType: (NSString *) anOrderType withOrderCount: (NSString *) anOrderCount hasOpenOrder: (BOOL) openOrder {
if ((self = [super init])) {
self.orderType = anOrderType;
self.orderCount = anOrderCount;
self.hasOpenOrder = openOrder;
}
return self;
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
self.lblOrderType.text =[NSString stringWithFormat:@"%@", self.orderType];
self.lblOrderCount.text = [NSString stringWithFormat:@"%@", self.orderCount];
if (self.hasOpenOrder) {
[self.orderButton setBackgroundImage:[UIImage imageNamed:@"background-order-btn-red.png"] forState:UIControlStateNormal];
self.lblOrderType.textColor = [UIColor whiteColor];
self.lblOrderCount.textColor = [UIColor whiteColor];
}
}
-(IBAction)btnTapped:(id)sender {
NSLog(@"TAPPED");
if ([self delegate]) {
[delegate tapped:sender withOrderType:self.orderType];
}
}
- (void)viewDidUnload
{
[self setOrderButton:nil];
[super viewDidUnload];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
@end
這似乎相當簡單,我在這裏做什麼,不知道什麼改變與ARC導致我的問題。
除去一些特定的按鍵確保'statsView ''不是'nil',並且當使用它的類被卸載或釋放時,嘗試將按鈕委託設置爲'nil'。 'unsafe_unretained'不會將像'weak'這樣的引用歸零。 – Joe
statsView是我主要的UIView中的屬性:@property(nonatomic,strong)IBOutlet UIView * statsView; – Slee
如果您忘記設置它,它仍然有可能是「無」。我只是建議你在將按鈕添加到它時確實確保它不是'nil'。 – Joe