以编程方式在UIViewController中添加UINavigationController

jos*_*lat 6 uitableview uinavigationcontroller pushviewcontroller ios4 ios

我目前有一个视图设置如下:

@interface BlogViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> {
    UITableView *mainTableView;
}

@property (nonatomic, retain) UITableView *mainTableView;
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,它内部有一个UITableView,我将所有数据加载到.但是,当我调用以下函数时:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    SingleBlogViewController *viewController = [[SingleBlogViewController alloc] init];
    [self.navigationController pushViewController:viewController animated:YES];
    //[self presentModalViewController:viewController animated:YES];
    [viewController release];
}
Run Code Online (Sandbox Code Playgroud)

什么都没发生.出于某种原因,我的UITableView内心UIViewController并没有推动观点.这是因为它不是UITableViewController吗?我尝试将其更改为,但仍然无法正常工作.

我在这里错过了什么吗?

Dav*_*ins 26

我发现这篇博文的第一部分对于展示如何在没有Interface Builder的情况下以编程方式创建和使用UINavigationController非常有用.

我希望文档和教程能够向我强调的一些事情:

  • 当你创建时,UINavigationController你可以免费获得UIView和UINavigationBar(即你不需要单独添加它们并弄清楚如何将它们连接在一起).

  • 您将该myUINavigationController.view属性添加为"主"视图,然后将/弹出/弹出UIViewController到UINavigationController上,它们将自动显示在myUINavigationController.viewUIView中可见.

  • 当你把a推UIViewController到a上时UINavigationController,UIViewController.navigationController就会被填入.如果你没有将视图推到导航控制器上,我猜这个属性是空的.

  • 以编程方式添加按钮的时间/地点UINavigationBar不是在构造时间和位置UINavigationController,而是UIViewController在您按下它们时加载它们时的个体UINavigationController.

  • 我只需要添加一个"完成"按钮到UINavigationControllerUINavigationBarviewDidLoad第一的方法UIViewController推到UINavigationController.任何UIViewController我推到第一个视图顶部的任何内容都会自动在"后退"按钮中导航到被遮盖的状态UIViewController.

  • 如果你在将它放在堆栈之前设置了你的title属性.该的头衔将是您的视图的标题.同样,任何"后退"按钮都会自动命名您要返回的视图,而不是一般地说"返回".UIViewControllerUINavigationControllerUINavigationBar