2014-01-11 95 views
0

我希望能夠在我的tableview中選擇一個項目,並獲得正確的視圖。 說我有兩個項目在我的uitableview。一個有Ubuntu的價值其他有一個拱門的價值 - 在我的plist從arraA我有一個SystemID的持有文本Ubuntu /拱,從來都沒有didSelectRowAtIndexPath或prepareForSegue使用?

現在當我選擇與Ubuntu的一個我希望它推動一個新的觀點,其中Ubuntu的東西是 ,如果我回去,並選擇拱它會再次推我到一個視圖,其中拱東西

到目前爲止,我做了這個,它不是如預期運行:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSString *segueName = nil; 

if ([[arrayA valueForKey:@"SystemID"]isEqualToString:@"Ubuntu Linux"]) { 
    ActionViewController *controller = [[ActionViewController alloc] initWithNibName:@"Ubuntu Linux" bundle:nil]; 
    controller.Serverinfo = [arrayA objectAtIndex:indexPath.row]; 
    [self.navigationController pushViewController:controller animated:YES]; 
} 
else if ([[arrayA valueForKey:@"SystemID"]isEqualToString:@"Arch Linux"]) { 
    ActionViewController *controller = [[ActionViewController alloc] initWithNibName:@"Arch Linux" bundle:nil]; 
    controller.Serverinfo = [arrayA objectAtIndex:indexPath.row]; 
    [self.navigationController pushViewController:controller animated:YES]; 
} 

[self performSegueWithIdentifier: segueName sender: self]; 
} 

也試過準備forsegue

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{ 
if([arrayA valueForKey:@"Arch Linux"]) 
{ 
    ActionViewController *controller = (ActionViewController *)segue.destinationViewController; 
    NSIndexPath *indexPath = [self.mainTableView indexPathForSelectedRow]; 
    controller.Serverinfo = [arrayA objectAtIndex:indexPath.row]; 
    NSLog(@"hostname = %@", controller.Serverinfo); 
} 
if ([arrayA valueForKey:@"Ubuntu Linux"]) { 
    ActionViewController *controller = (ActionViewController *)segue.destinationViewController; 
    NSIndexPath *indexPath = [self.mainTableView indexPathForSelectedRow]; 
    controller.Serverinfo = [arrayA objectAtIndex:indexPath.row]; 
    NSLog(@"hostname = %@", controller.Serverinfo); 
} 
} 

arrayA是NSMutableArray的是從plist中讀取,你可以在這裏看到 nib

+0

是一個數組NSAray或NSMutableArray的類型?你在arrayA中保存兩個NSString類型的對象嗎? – Greg

+0

我想你需要在這段代碼中使用'indexPath'或'sender'來知道哪一行被選中。 –

+0

arrayA是一個NSMutableArray。兩個具有不同值的對象 – KennyVB

回答

1

替換你的方法:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

if ([arrayA[indexPath.row][@"SystemID"]isEqualToString:@"Ubuntu Linux"]) { 
    //ActionViewController *controller = [[ActionViewController alloc] initWithNibName:@"Ubuntu Linux" bundle:nil]; 
    // Make sure you replace Storyboard with your storyboard name 
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Storyboard" bundle:nil]; 
    ActionViewController *controller = [storyboard instantiateViewControllerWithIdentifier:@"Ubuntu Linux"]; 
    controller.Serverinfo = [arrayA objectAtIndex:indexPath.row]; 
    [self.navigationController pushViewController:controller animated:YES]; 
} 
else if ([arrayA[indexPath.row][@"SystemID"]isEqualToString:@"Arch Linux"]) { 
    ActionViewController *controller = [[ActionViewController alloc] initWithNibName:@"Arch Linux" bundle:nil]; 
    controller.Serverinfo = [arrayA objectAtIndex:indexPath.row]; 
    [self.navigationController pushViewController:controller animated:YES]; 
} 
} 

你並不需要調用[self performSegueWithIdentifier: segueName sender: self];在didSelectRow因爲你已經推didSelectRow方法您的視圖。如果你打電話給它,你會放兩次視圖。

+0

我試着用你的代碼。它怎麼都找不到NIB?我更新了我的問題以反映我的筆尖被稱爲什麼。開始得到一點困惑,如果我把它命名爲正確的地方 – KennyVB

+0

我想我需要使它成爲一個故事板ID以某種方式與您發佈的代碼。你能幫忙嗎? thx到目前爲止。或者我可以實際創建一個筆尖。 – KennyVB

+0

@KennyVB你必須從storyboard加載你的視圖控制器。查看Ubuntu Linux的示例並應用Arch Linux的邏輯。 – Greg

0

使用故事板與prepareForSegue

+0

我怎樣才能使它有兩個選項? – KennyVB

1

enter image description here

哦男孩,你掉的雜草。

首先,停止談論推動「觀點」。術語視圖和視圖控制器是不可互換。視圖是顯示在屏幕上的對象。視圖CONTROLLER是管理視圖集合(通常是整個屏幕)的對象。

您推送視圖控制器,而不是視圖。視圖控制器然後在屏幕上顯示它的內容視圖。

現在,至於你的代碼。

您的tableView:didSelectRowAtIndexPath:方法中的if語句已混亂。

if ([[arrayA valueForKey:@"SystemID"]isEqualToString:@"Ubuntu Linux"]) 

您不想在陣列上使用valueForeKey。該方法嘗試對數組中的每個對象調用valueForKey,並返回結果數組。

數組通過數字索引索引,而不是索引。你想要一個數組作爲表視圖的數據源。

你想要做的是從你的數組中取出你所選擇的行的對象。

但是,爲了進一步幫助您,您需要解釋您存儲在陣列A中的內容。這是一組字典嗎?一串字符串?

此外,您正在混合基於NIB的視圖控制器代碼與故事板代碼。如果你需要的話,你當然可以在同一個程序中使用它們,但作爲初學者你可能不應該這樣做。

在你的tableView:didSelectRowAtIndexPath方法中,你正在創建一個視圖控制器initWithNibName,推動它,然後調用performSegueWithIdentifier。你想要做一個或另一個,而不是兩個。

+0

soo教我或指出我應該使用什麼方向以及如何使它正常工作......因爲現在。我迷路了 – KennyVB

+0

肯尼,格雷格的回答給你你需要的一切。 –

相關問題