2014-07-12 35 views
0

我是全新的應用程序開發,所以這可能是一個愚蠢的問題。所以我做了一個UI表。每一行都是不同的主題。我想允許用戶點擊一個表格單元格,並將它們引導到另一個視圖控制器。所有視圖控制器將以不同的方式排列不同的內容。任何想法如何實現這個使用故事板或只是編程?欣賞它!如何創建視圖控制器數組?

+0

沒有任何理由爲此創建一個視圖控制器數組。 – rmaddy

回答

2

要回答這個崗位的主要問題,這裏是你如何創建視圖控制器的陣列:

// create your view controllers and customize them however you want 
UIViewController *viewController1 = [[UIViewController alloc] init]; 
UIViewController *viewController2 = [[UIViewController alloc] init]; 
UIViewController *viewController3 = [[UIViewController alloc] init]; 

// create an array of those view controllers 
NSArray *viewControllerArray = @[viewController1, viewController2, viewController3]; 

我不敢肯定這是你真正需要做的給你解釋什麼,但沒有更多的信息,這回答了最初的問題。

你真的不想一次創建所有的視圖控制器,並讓它們坐在內存中 - 你真的只想在實際需要時創建它們 - 當用戶選擇單元時。你會想要做下面的事情來實現你想要做的事情:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    [tableView deselectRowAtIndexPath:indexPath animated:YES]; 

    if (indexPath.row == 0) { 
     // create the view controller associated with the first cell here 
     UIViewController *viewController1 = [[UIViewController alloc] init]; 
     [self.navigationController pushViewController:viewController1 animated:YES]; 
    } 

    else if (indexPath.row == 1) { 
     // create the view controller associated with the second cell here 
     UIViewController *viewController2 = [[UIViewController alloc] init]; 
     [self.navigationController pushViewController:viewController2 animated:YES]; 
    } 

    else { 
     // etc 
    } 
} 
+2

對。基本的答案就是這麼簡單。然而,可能更好地以「按需」創建它們。 –

+0

確實 - 增加了更多的信息,因爲我認爲這可能更符合用戶的需求。 – Mike

相關問題