如何创建一个视图控制器数组



我是应用开发的新手,所以这可能是个愚蠢的问题。我做了一个UI表。每一行都是一个不同的主题。我想让用户点击一个表格单元格它会引导他们到另一个视图控制器。所有的视图控制器都会有不同的内容以不同的方式排列。任何想法如何实现这个使用故事板或只是编程?很感激!

为了回答这篇文章的主要问题,下面是如何创建视图控制器数组:

// 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
    }
}

最新更新