将 UIViewController 推送到 UINavigationController 上

发布于 2024-10-10 07:46:59 字数 677 浏览 2 评论 0原文

有一天,我询问如何使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

何以笙箫默 2024-10-17 07:46:59

嗯,如果不知道实现的细节,这有点棘手——我假设您按照链接文章中的方式实现了导航控制器。另外,虽然您没有提供任何细节,但听起来您已经在某处添加了一个表视图控制器,因此我使 UIViewController 符合 UITableView 协议,以便在一个地方处理所有内容:

@interface SOViewController : UIViewController<UITableViewDelegate,UITableViewDataSource>; {

  UINavigationController* navController;
}

- (IBAction) pushMe:(id)sender;
@end

我在 IB 中的 SOViewController 视图上放置了一个按钮,并将 PushMe: 操作连接到它。我还创建了另一个名为 JunkController 的基于 UIViewController 的类,并在 IB 中的视图上放置了一个“垃圾”标签 —— 这就是我在 IB 中所做的一切。在SOViewController的viewDidLoad:中的

navController = [[[UINavigationController alloc] init] retain];
navController.navigationBar.barStyle = UIBarStyleBlackOpaque;
navController.toolbarHidden = YES;

UITableViewController* tvController = [[UITableViewController alloc] init];
UITableView* tv = [[UITableView alloc] init];
tvController.tableView = tv;
tv.delegate = self;
tv.dataSource = self;
[navController setViewControllers:[NSArray arrayWithObject:tvController]];

pushMe:action实现中:

[selfpresentModalViewController:navControlleranimated:YES];

实现了tableViewdelegate和datasource方法;供选择:

- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath  { 
    NSLog(@"row selected");
    JunkController* junk = [[JunkController alloc] initWithNibName:@"junkcontroller" bundle:nil];
    [navController pushViewController:junk animated:YES];
    [junk release];
}

这应该会生成一个应用程序,该应用程序的屏幕上带有“推我”按钮。当按下该按钮时,您应该得到一个基于动画模态导航的表格视图——我的表格视图中有一行包含一个标签“选择我”。触摸这一行应该会将垃圾控制器动画化到视图中。

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 > {

  UINavigationController* navController;
}

- (IBAction) pushMe:(id)sender;
@end

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:

navController = [[[UINavigationController alloc] init] retain];
navController.navigationBar.barStyle = UIBarStyleBlackOpaque;
navController.toolbarHidden = YES;

UITableViewController* tvController = [[UITableViewController alloc] init];
UITableView* tv = [[UITableView alloc] init];
tvController.tableView = tv;
tv.delegate = self;
tv.dataSource = self;
[navController setViewControllers:[NSArray arrayWithObject:tvController]];

In the pushMe: action implementation:

[self presentModalViewController:navController animated:YES];

Implemented the tableView delegate and datasource methods; for selection:

- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath  { 
    NSLog(@"row selected");
    JunkController* junk = [[JunkController alloc] initWithNibName:@"junkcontroller" bundle:nil];
    [navController pushViewController:junk animated:YES];
    [junk release];
}

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.

紫瑟鸿黎 2024-10-17 07:46:59

无需在此视图控制器中声明 setupViewController 属性。另外,我可能会弄错,但我认为“控制器”是 Cocoa 中的保留名称,我会更改该名称。因此,请确保您已注册 UITableViewDelegate 并使用 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 挂钩并推送新的视图控制器,如下所示:

SetupViewController *detailViewController = [[SetupViewController alloc] initWithNibName:@"SetupViewController" bundle:nil];
detailViewController.title = @"Setup";
[self.navigationController pushViewController:detailViewController animated:YES];
[detailViewController release];

祝你好运!

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:

SetupViewController *detailViewController = [[SetupViewController alloc] initWithNibName:@"SetupViewController" bundle:nil];
detailViewController.title = @"Setup";
[self.navigationController pushViewController:detailViewController animated:YES];
[detailViewController release];

Goodluck!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文