2012-01-12 108 views
2

我正在創建一個拆分式iPad應用程序。當用戶按下主視圖中的條形按鈕項時,會顯示一個模式。這種模式有一個文本框,並有一個IBAction來獲取鍵盤返回。應用程序在設置UITableViewCell時發生崩潰:textLabel:text

在鍵盤返回時,會創建一個新的Farm類實例(代碼如下)。然後將此實例添加到存儲在我的委託中的數組中。然後我嘗試重新加載MasterViewController的桌子。在此重新加載應用程序崩潰cell.textLabel.textEXC_BAD_ACCESS錯誤。

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

如果我問委託內的數組有多少個元素,它甚至會顯示當前的數量。對我而言,這是一件奇怪的事情:農場實例似乎已經存在。

我在我的MasterViewController和我的NewFarmNamingView類都有AppDelegate的實例。 Master中的實例是填充表格。 NewFarm中的實例是將新創建的Farm添加到委託。下面的代碼。

從類NewFarmNamingView細分:從類農場

- (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; 
} 
+0

您在調試控制檯中遇到的確切錯誤是什麼? – UIAdam 2012-01-12 04:23:09

+0

我用收到的信號更新了我的問題。調試堆棧的頂部是「[UILabel setText:]」頂部的「objc_msgSend」 – 2012-01-12 04:27:59

+0

getFarmTitle可能被破壞了嗎?嘗試將其包裝在一個respondsToSelector:方法中。 – CodaFi 2012-01-12 04:31:37

回答

1

從運行時參考

- (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]; 
} 

段:「objc_msgsend將消息發送到所述接收器,並期望一個簡單的返回值。」

我會打賭你在返回任何東西(如果你返回任何東西)在該方法getTitleFarm返回一個不正確的值。它應該是一個NSString。絕對確定它是返回一個NSString,而不是其他任何東西。

如果您需要使用respondsToSelector方法來查看類是否被釋放,請嘗試:

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

編輯:也許你不保留,甚至不惜一切創造這個字符串。在初始化過程中,將其封裝在retain];消息中

+1

NSString修復了這種情況,你是一個救命恩人!:-) – 2012-01-12 04:54:29

+0

沒問題。祝你好運。 – CodaFi 2012-01-12 04:58:56

相關問題