2012-08-23 24 views
1

我有一個創建的類來生成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導致我的問題。

+0

除去一些特定的按鍵確保'statsView ''不是'nil',並且當使用它的類被卸載或釋放時,嘗試將按鈕委託設置爲'nil'。 'unsafe_unretained'不會將像'weak'這樣的引用歸零。 – Joe

+0

statsView是我主要的UIView中的屬性:@property(nonatomic,strong)IBOutlet UIView * statsView; – Slee

+0

如果您忘記設置它,它仍然有可能是「無」。我只是建議你在將按鈕添加到它時確實確保它不是'nil'。 – Joe

回答

1

也許ARC自動釋放創建的按鈕,嘗試創建按鈕,存儲陣列

//.h file 
@property (nonatomic, strong) NSArray *buttonsArray 

//.m file 
@synthesize buttonsArray 
... 
- (void)viewDidLoad { 
    buttonsArray = [NSArray array]; 
... 
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]; 
//Add button to array 
[buttonsArray addObject:btn]; 

而且,這種方法會幫助,如果你想改變的按鈕,或者從視圖

+0

這樣做,似乎是一些奇怪的行爲 - 不知道這是否會在我的應用程序彈出:( – Slee

+0

@Slee這不應該是必要的,你應該嘗試追查這個問題的原因。 – Joe

相關問題