简体   繁体   English

如何以编程方式创建UIViewController?

[英]How do I create a UIViewController programmatically?

I am working in a app where i have data in UITableView . 我正在一个应用程序中工作,我在UITableView有数据。 It is like a drill down application. 它就像一个向下钻取的应用程序。 User will click on a row and will go to next page showing more records in a UITableView . 用户将单击一行,将转到下一页,在UITableView显示更多记录。 But problem in my case is that i dont know upto how many level user can drill. 但我的问题是,我不知道用户可以钻多少级别。 The number of levels are not fixed. 级别数量不固定。 So now i am thinking to create and add the viewcontrollers programmatically. 所以现在我想以编程方式创建和添加viewcontrollers。 Is it possible?? 可能吗?? if yes how? 如果有,怎么样? thanks in advance. 提前致谢。

UIViewController *controller = [[UIViewController alloc] init];
controller.view = whateverViewYouHave;

Do you have your own view controller that you coded? 您是否拥有自己编码的视图控制器? In that case you probably don't need to set the view property as it has been set in IB if that is what you used. 在这种情况下,您可能不需要设置视图属性,因为它已在IB中设置,如果这是您使用的。 When you have your controller you can push it onto the navigationController or view it modally etc. 拥有控制器后,可以将其推送到navigationController或以模态方式查看。

UIViewController s are always created programmatically. UIViewController总是以编程方式创建的。 It sounds like you just need to have the same class for each level of view controller, eg: 听起来你只需要为每个级别的视图控制器都有相同的类,例如:

//CoolViewController:UITableViewController
//CoolViewController.m
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (!self.isAtTopLevel) {
        CoolViewController *cvc = [[CoolViewController alloc] initWithRecord:[self.records objectAtIndex:indexPath.row]];
        [self.navigationController pushViewController:cvc animated:YES];
        [cvc release];
    } else {
        //do something else
    }
}

In this case, thingies would be some sort of recursive NSArray (ie an array of arrays). 在这种情况下, thingies将是某种递归NSArray(即数组数组)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM