App崩溃设置UITableViewCell:textLabel:text

时间:2012-01-12 04:19:08

标签: iphone objective-c ios ipad uitableview

我正在创建一个拆分视图的iPad应用程序。当用户按下主视图中的条形按钮项时,呈现模态。这个模态有一个文本字段,并有一个IBAction来获取键盘返回。

在键盘返回时,创建了我的Farm类的新实例(下面的代码)。然后将此实例添加到存储在我的委托中的数组中。然后我尝试重新加载MasterViewController的表。重新加载后,应用程序在cell.textLabel.text上崩溃并出现EXC_BAD_ACCESS错误。

Farm *current = [delegate.arrayOfFarms objectAtIndex:indexPath.row];
cell.textLabel.text = [current getFarmTitle];

如果我问代表中的数组有多少元素,它确实会显示当前的数量,甚至。对于整个事情,这对我来说是奇怪的:农场实例似乎存在。

我的MasterViewControllerNewFarmNamingView课程都有AppDelegate实例。 Master中的实例是填充表格。 NewFarm中的实例是将新创建的Farm添加到委托中。代码如下。

NewFarmNamingView类的细分:

- (IBAction) keyboardDonePushed:(id)sender
{
    // create a Farm and add it to the delegate
    NSString *text = newFarmTextField.text;
    Farm *newFarm = [[Farm alloc] init];
    [newFarm setFarmTitle:text];
    [[delegate arrayOfFarms] addObject:newFarm];
    [newFarm release];

    NSLog(@"Added farm: %@" , text);

    // dismiss the view
    [self closeView:nil];
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    // initialize the delegate
    delegate = [[UIApplication sharedApplication] delegate];
}

来自类Farm的细分

- (void) setFarmTitle : (NSString *) _farmTitle
{
    farmTitle = _farmTitle;
}

- (NSString *) getFarmTitle
{
    return farmTitle;
}

// NSCoding Methods
- (void) encodeWithCoder:(NSCoder *)aCoder
{
    [aCoder encodeObject:farmTitle forKey:@"kFarmTitle"];
}

- (id) initWithCoder:(NSCoder *)aDecoder
{
    farmTitle = [aDecoder decodeObjectForKey:@"kFarmTitle"];
    return self;
}

// Initialization method
- (id)init
{
    self = [super init];
    if (self) {
        // Initialization code here.
    }

    return self;
}

1 个答案:

答案 0 :(得分:1)

从运行时引用:“objc_msgsend向接收者发送一条消息并期望一个简单的返回值。”

我会打赌你在那个类方法getTitleFarm返回一个不正确的值时,你要返回的东西(如果你要返回任何东西)。它应该是一个NSString。绝对肯定它会返回一个NSString,而不是其他任何东西。

如果您需要使用respondsToSelector方法查看该类是否正在发布,请尝试:

if([current respondsToSelector:@selector(getFarmTitles)])  {.    [current getFarmTitle];
} 
else {
NSLog:(@"FAILURE!!");
}

编辑:也许你根本就没有保留甚至创建这个字符串。在它的初始化中,将其包装在retain];消息中