xcode IOS:在子视图中使用导航控制器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6189105/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
IOS: use navigation controller in a subview
提问by cyclingIsBetter
In a view of my program I have a button and with this button I open a subview; this subview is a view with a tableview. I want go to another view when I push a row of the tableview so I want to make this with a navigation controller; how can I do this?
在我的程序视图中,我有一个按钮,用这个按钮打开一个子视图;这个子视图是一个带有 tableview 的视图。当我推一行 tableview 时,我想转到另一个视图,所以我想用导航控制器来做这个;我怎样才能做到这一点?
回答by sergio
Your table view delegate will receive a tableView:didSelectRowAtIndexPath:
message when you click on one of the table's rows.
tableView:didSelectRowAtIndexPath:
当您单击表的行之一时,您的表视图委托将收到一条消息。
You can put there your code to create the UINavigationController and push on to it your new view.
你可以把你的代码放在那里来创建 UINavigationController 并将你的新视图推送到它。
This sample code (from another answer of mine on S.O.) shows how you can do that:
这个示例代码(来自我在 SO 上的另一个答案)展示了如何做到这一点:
UINavigationController* navigation = [[UINavigationController alloc] init];
iVkViewController *overviewViewController = [[iVkViewController alloc] init];
overviewViewController.title = @"First";
[navigation pushViewController:overviewViewController animated:NO];
This should help you getting things on track.
这应该可以帮助您使事情走上正轨。
One side note: you might think of having a navigation controller from the very start, this would make your UI more "well-behaved", but this depends ultimately on your app requirements.
一方面:您可能会从一开始就考虑使用导航控制器,这将使您的 UI 更加“乖巧”,但这最终取决于您的应用程序要求。
回答by ElonChan
If UIViewController addSubView a view of UITableViewController,you want to push in the method of tableView:didSelectRowAtIndexPath,you should check if self.navigationController is nil. If it's nil,You probably should use
如果UIViewController addSubView 一个UITableViewController 的视图,你想在tableView:didSelectRowAtIndexPath 方法中push,你应该检查self.navigationController 是否为nil。如果它为零,您可能应该使用
[self.parentViewController.navigationController pushViewController:controller animated:YES];
if self.parentViewController is also nil,Sometimes,you have to set a @property to point out the parentViewController In UITableViewController,like:
如果 self.parentViewController 也是 nil,有时,你必须设置一个@property 来指出 UITableViewController 中的 parentViewController,例如:
@property (nonatomic, weak) UIViewController *parentVC;
and in UIViewController:
在 UIViewController 中:
UITableViewController *tableViewVC = [[UITableViewController alloc] init];
tableViewVC.parentVC = self;
In UITableViewController ,-tableView:didSelectRowAtIndexPath:
在 UITableViewController 中,-tableView:didSelectRowAtIndexPath:
[self.parentVC.navigationController pushViewController:controller animated:YES];