如何以编程方式创建视图层次结构?

时间:2011-03-20 15:36:09

标签: ios ios4 ipad

我通常在IB中创建我的视图层次结构,但这次我需要在我的代码中创建它。基本上我已经拥有自定义视图,但我希望它包含在UINavigationController中。那我怎么能这样做呢?

2 个答案:

答案 0 :(得分:3)

如果您希望将其嵌套在导航控制器中,则应使用:

   UIViewController * myViewController = [[GameController alloc] init];
    myViewController.view = yourCustumeView;//if you are trying to add a UIView
   UINavigationController *navigationController = [[UINavigationController alloc]
                                          initWithRootViewController:myViewController];
   [self.navigationController pushViewController:navigationController animated:YES];
   [navigationController  release];
   [myViewController  release];
祝你好运

修改 添加此代码(在发布之前):

    navigationController.navigationItem.leftBarButtonItem=nil;//change it to rightBarButtonItem if the button is on the right.

答案 1 :(得分:0)

如果你想创建几个ViewControllers然后你可以在代码中分配它们,然后为了显示它们就像这样推送它:

RegistrationViewController * regView= [[RegistrationViewController alloc] init];
[self.navigationController pushViewController:regView animated:YES];