1

我有一个我创建的类来生成我添加到我的 UIView 的 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 发生了什么变化导致我出现问题。

4

1 回答 1

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];

如果您想更改按钮或从视图中删除某些特定按钮,这种方法也会有所帮助

于 2012-08-23T14:03:45.667 回答