我创建了一个类来生成我添加到我的 UIView 的 UIButton。在我昨天转换为 ARC 之前,这一直很有效,但我没有收到以下错误:
-[OrderTypeButton performSelector:withObject:withObject:]: message sent to deallocated instance 0x12449f70
这是将按钮添加到我的 UIView 的代码(实际上是我的主 UIView 中的一个 subview ):
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];
这是我的类(class)标题:
#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 导致了我的问题。
最佳答案
也许 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];
如果您想更改按钮或从 View 中删除某些特定按钮,这种方法也会有所帮助
关于ios - iOS 5 ARC 中动态生成的 UIButton 导致释放的实例崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12093126/