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 に答える