将 UIViewController 推送到 UINavigationController 上
有一天,我询问如何使用 UINavigationController 作为 UIViewController 的子级。我通过答案得到了这个工作。现在我想做的是将控制器推送到导航堆栈上。当触摸表格单元格时,我会执行以下操作:
- (void) showSetup {
NSLog(@"Showing Setup");
SetupViewController *controller = [[SetupViewController alloc]initWithNibName:@"SetupViewController" bundle:nil];
self.setupViewController = controller;
self.setupViewController.title = @"Setup";
[self.navigationController pushViewController:self.setupViewController animated:YES];
[controller release];
}
我可以在控制台中看到日志语句,但视图永远不会改变。我错过了什么吗?
The other day I asked about using a UINavigationController as a child of a UIViewController. I got that working via the answer. Now what I'm trying to do is push a controller onto the nav stack. When a table cell is touched, I do the following:
- (void) showSetup {
NSLog(@"Showing Setup");
SetupViewController *controller = [[SetupViewController alloc]initWithNibName:@"SetupViewController" bundle:nil];
self.setupViewController = controller;
self.setupViewController.title = @"Setup";
[self.navigationController pushViewController:self.setupViewController animated:YES];
[controller release];
}
I can see the log statement in my console, but the view never changes. Am I missing something?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
嗯,如果不知道实现的细节,这有点棘手——我假设您按照链接文章中的方式实现了导航控制器。另外,虽然您没有提供任何细节,但听起来您已经在某处添加了一个表视图控制器,因此我使 UIViewController 符合 UITableView 协议,以便在一个地方处理所有内容:
@interface SOViewController : UIViewController<
UITableViewDelegate,UITableViewDataSource>; {
我在 IB 中的 SOViewController 视图上放置了一个按钮,并将 PushMe: 操作连接到它。我还创建了另一个名为
JunkController
的基于 UIViewController 的类,并在 IB 中的视图上放置了一个“垃圾”标签 —— 这就是我在 IB 中所做的一切。在SOViewController的viewDidLoad:中的pushMe:action实现中:
[selfpresentModalViewController:navControlleranimated:YES];
实现了tableViewdelegate和datasource方法;供选择:
这应该会生成一个应用程序,该应用程序的屏幕上带有“推我”按钮。当按下该按钮时,您应该得到一个基于动画模态导航的表格视图——我的表格视图中有一行包含一个标签“选择我”。触摸这一行应该会将垃圾控制器动画化到视图中。
Hmmm, well it's a bit tricky without knowing the details of your implementation -- I assumed that you implemented your navigation controller as in the linked article. Also although you give no details it sounds like you've added a table view controller somewhere along the line, so I made the UIViewController conform to the UITableView protocols to handle everything in one place:
@interface SOViewController : UIViewController
<UITableViewDelegate,UITableViewDataSource > {
I dropped a button on the SOViewController's view in IB and wired the pushMe: action to it. I also created another UIViewController-based class called
JunkController
and dropped a "Junk" label on it's view in IB -- that's all I did in IB. In the SOViewController's viewDidLoad:In the pushMe: action implementation:
[self presentModalViewController:navController animated:YES];
Implemented the tableView delegate and datasource methods; for selection:
This should yield an app that surfaces a screen with a "Push me" button. When that button is pressed you should get an animated modal navigation-based table view -- mine had one row in it that contained a label "select me". Touching this row should animate the junk controller into view.
无需在此视图控制器中声明 setupViewController 属性。另外,我可能会弄错,但我认为“控制器”是 Cocoa 中的保留名称,我会更改该名称。因此,请确保您已注册 UITableViewDelegate 并使用
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
挂钩并推送新的视图控制器,如下所示:祝你好运!
There is no need to make setupViewController a declared property in this view controller. Also, I could be mistaken but I thought "controller" was a reserved name in Cocoa, I'd change that name. So make sure you have registered with the UITableViewDelegate and use
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
to hook into and push your new view controller as follows:Goodluck!