ios - iOS 5 ARC 中动态生成的 UIButton 导致释放的实例崩溃

标签 ios objective-c ios5 automatic-ref-counting

我创建了一个类来生成我添加到我的 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/

相关文章:

ios - 是否有 iOS 加速方法来执行元素最大(np.maximum)?

ios - 在 tableview 单元格中使用 KVO 跟踪对 Swift 3 中类实例的特定属性的更改

ios - iPhone 崩溃时删除的核心数据对象

objective-c - ios打破嵌套循环

ios - 应用内购买下载永远不会被下载

ios - 在 ios uitableview 中间歇性地获取 json 提要失败

ios - 自定义 UITableViewCell、UITableView 和 allowsMultipleSelectionDuringEditing

ios - 分段控制 TableView 的自定义编辑按钮

NSURLConnection 异步请求期间的 iOS 7 更新 UI

ios - 努力从 NSArray 填充 UITableView