iOS 5 ARC中动态生成的UIButton导致释放实例崩溃
我创建了一个类来生成我添加到UIView中的UIButton。这在我昨天转换为ARC之前一直非常有效,但我得到以下错误:iOS 5 ARC中动态生成的UIButton导致释放实例崩溃,ios,objective-c,ios5,automatic-ref-counting,Ios,Objective C,Ios5,Automatic Ref Counting,我创建了一个类来生成我添加到UIView中的UIButton。这在我昨天转换为ARC之前一直非常有效,但我得到以下错误: -[OrderTypeButton performSelector:withObject:withObject:]: message sent to deallocated instance 0x12449f70 以下是将按钮添加到我的UIView(实际上是我的主UIView中的子视图)的代码: 这是我的课程标题: #import <UIKit/UIKit.h>
-[OrderTypeButton performSelector:withObject:withObject:]: message sent to deallocated instance 0x12449f70
以下是将按钮添加到我的UIView(实际上是我的主UIView中的子视图)的代码:
这是我的课程标题:
#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
我在这里做的事情似乎相当简单,不确定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];
此外,如果您想更改按钮,或从视图中删除某些特定按钮,此方法也会有所帮助。可能是自动释放创建的按钮,尝试将创建的按钮存储在数组中
//.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];
此外,如果要更改按钮,或从视图中删除某些特定按钮,此方法也会有所帮助确保
statsView
不是nil
,并在卸载或解除分配使用按钮的类时尝试将按钮委托设置为nil
<代码>不安全\未恢复不会像弱
引用那样将引用归零。statsView是我的主UIView中的一个属性:@property(非原子,强)IBOutlet UIView*statsView;如果您忘记设置它,它仍然可能是nil
。我只是建议您在添加按钮时确实确保它不是nil
。我将btn添加到may view self.view中,但仍然收到错误,因此我认为不是statsView设置断点并尝试检查或打印以查看是谁在接收导致崩溃的呼叫。确保statsView
不是nil
并在卸载或解除分配使用它的类时尝试将按钮委托设置为nil
<代码>不安全\未恢复不会像弱
引用那样将引用归零。statsView是我的主UIView中的一个属性:@property(非原子,强)IBOutlet UIView*statsView;如果您忘记设置它,它仍然可能是nil
。我只是建议您在添加按钮时确保它不是nil
。我将btn添加到may view self.view中,但仍然收到错误,因此我不认为是StatView设置断点并尝试检查或打印以查看是谁在接收导致崩溃的呼叫。确实如此,似乎有一些奇怪的行为-想知道这将在我的应用程序中弹出到哪里:(@Slee这不必要,你应该尝试跟踪此问题的原因。确实如此,似乎有一些奇怪的行为-想知道这将在我的应用程序中弹出到哪里:(@Slee这应该不是必需的,您应该尝试查找此问题的原因。
//.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];