2013-04-18 31 views
0

我花了幾個小時試圖解決這個問題 我試圖初始化類參數Init方法與參數不叫

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    //Fetch the value of the selected row in NSString 
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 
    // NSString *tableRowValue = cell.text; 
    tableRowValue = [[listOfItems objectAtIndex:indexPath.row]objectForKey: @"clientId"]; 


    //Call the next view 

    ContactsList *contactsList = [[ContactsList alloc] initWithNibName:NSStringFromClass([ContactsList class]) bundle:nil]; 
    @try { 

     [self presentModalViewController:contactsList animated:YES]; 
    } 
    @catch (NSException *ex) { 
     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:[NSString stringWithFormat:@"%@",ex] 
                 delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil]; 
     [alert show]; 
    } 



    contactsList.modalTransitionStyle = UIModalTransitionStyleCrossDissolve ; 

    //Initialize the view with the clicked row value thwough NSString variable 
    [contactsList init:tableRowValue]; 

} 

這行代碼

[contactsList init:tableRowValue]; 

我得到以下警告:未使用表達式結果

和ContactsList類中的init方法未被調用

感謝您的幫助

+1

如果你想壓制警告,你可以用(void)加入。我會改變你的方法名稱。 init用於實例化對象。如果你想添加一個變量,然後使用'intiWithTableRowValue:tableRowValue' – Fogmeister

+0

謝謝你,我編輯了我的代碼。在init方法 – janeloulou

+1

之前調用ViewDidLoad方法,因爲您在intiWithTableRowValue之前調用presentModalViewController。 – Rajneesh071

回答

0

ContactList對象已在initWithNibName:方法中初始化,該方法依次調用從NSObject繼承的init方法(不帶參數)。

如果您需要在初始化後設置您的視圖,請創建一個名爲「setupWithTableRowValue:」或類似的方法。

0

你不應該這樣叫init。在分配對象之後應該首先調用init,並且永遠不會再調用該對象。在你的情況下,init已被稱爲initWithNibName

改爲使用一些setter。你的setter不需要返回任何東西。 init總是返回id。所以 contactsList = [contactsList init:tableRowValue]; 會很好IF,並且只有當它在 之後被直接調用時contactList = [ContactsList alloc]; ,因爲init實際上可能會返回一個不同於它被調用的對象。你沒有使用返回值,因此可能 - 理論上 - 放棄你的對象。

再次:使用setter或類似的方法將tableRowValue傳遞到contactsList