Objective-C NSMutableArray Count导致EXC_BAD_ACCESS

时间:2010-05-20 03:17:25

标签: objective-c nsmutablearray uipickerview exc-bad-access uipopovercontroller

我已经坚持了好几天,每次回到它我都会让我的代码越来越让我自己感到困惑,哈哈。这就是我想要做的。我有一个收费表,我点击一个,然后打开带收费详情的模型视图。现在,当模型被呈现时,创建一个对象来获取XML用户列表并解析它并通过自定义委托返回NSMutableArray。然后我有一个提示选择器弹出窗口的按钮,当调用弹出窗口视图时,用户数组用于对弹出窗口视图的initWithArray调用。我知道数组中的数据是正确的,但是当调用[pickerUsers count]时,我得到一个EXC_BAD_ACCESS。我认为这是一个内存/所有权问题,但似乎没有任何帮助。任何帮助将不胜感激。

相关代码段:

Charge Popover(收费详情模型视图):

@interface ChargePopoverViewController .....
NSMutableArray *pickerUserList;
@property (nonatomic, retain) NSMutableArray *pickerUserList;

@implementation ChargePopoverViewController
@synthesize whoOwesPickerButton, pickerUserList;
- (void)viewDidLoad {   
    JEHWebAPIPickerUsers *fetcher = [[JEHWebAPIPickerUsers alloc] init];
    fetcher.delegate = self;
    [fetcher fetchUsers];
}
-(void) JEHWebAPIFetchedUsers:(NSMutableArray *)theData {
    [pickerUserList release];
    pickerUserList = theData;
}
- (void) pickWhoPaid: (id) sender {
    UserPickerViewController* content = [[UserPickerViewController alloc] initWithArray:pickerUserList];
    UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:content];
    [popover presentPopoverFromRect:whoPaidPickerButton.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
    content.delegate = self;    
}

用户选择器视图控制器

@interface UserPickerViewController .....
NSMutableArray *pickerUsers;
@property(nonatomic, retain) NSMutableArray *pickerUsers;

@implementation UserPickerViewController
@synthesize pickerUsers;
-(UserPickerViewController*) initWithArray:(NSMutableArray *)theUsers {
    self = [super init];

    if ( self ) {
        self.pickerUsers = theUsers;
    }

    return self;
}
- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component {
    // Dies Here EXC_BAD_ACCESS, but NSLog(@"The content of array is%@",pickerUsers); shows correct array data 
    return [pickerUsers count];
}

如果可能有帮助,我可以提供其他代码。提前谢谢。

1 个答案:

答案 0 :(得分:3)

你声明ivar将数组保存为......

@property (nonatomic, retain) NSMutableArray *pickerUserList;

但是你有一个像这样实现的方法:

-(void) JEHWebAPIFetchedUsers:(NSMutableArray *)theData {
    [pickerUserList release];
    pickerUserList = theData;
}

您没有保留theData并且您没有调用合成的setter。如果你做了构建和分析,它应该抓住这个问题并告诉你它。如果没有,请提交错误。