Add a UINavigationBar to a UITableViewController without a UINavigationController

左心房为你撑大大i 提交于 2019-12-01 01:08:10

问题


I have an existing UITableViewController that was previously being used in a UINavigationController.

I need to convert it to be presented as a modal view. However, I still want to have a navigation bar at the top. I know this sounds strange- why not present it in the UINavigationController if I want a UINavBar? I want to present it without the UITabBarController that is associated with my UINavigationController.

I tried opening the XIB, adding a new view, moving the UITableView to be a subview and adding a NavigationBar to that new view as well. However this doesn't seem to make any impact and the whole tableview is still presented - no nav bar is visible. I think this is because the class is a subclass of UITableViewController.

Do I need to convert this to a UIViewClass? Is there a good approach to adding a navbar in code or via Interface Builder to an existing UITableViewController?

Thanks for any advice on how to approach this.


回答1:


Did you change the connection in the XIB for the File's Owner view? It should point to your outer view which contains both the navbar and tableview.

But I'm not sure I understand why you don't want to use a navigation controller. Just do this:

MyViewController *viewController = [[[MyViewController alloc] init] autorelease];
UINavigationController *navController = [[[UINavigationController alloc] initWithRootViewController:viewController] autorelease];
[self presentModalViewController:navController animated:YES];

I do this all the time when presenting a modal view - it seems cleaner than including a navbar directly in the view.



来源:https://stackoverflow.com/questions/4035370/add-a-uinavigationbar-to-a-uitableviewcontroller-without-a-uinavigationcontrolle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!