父视图navigationController

发布于 2024-10-18 01:50:15 字数 497 浏览 7 评论 0原文

我有一个视图,它是 tabBarController 的一部分。在此视图中,我有一个带有表格的子视图。单击此表中的单元格时,我想访问父视图的导航控制器。这可能吗?如果可能的话,如何实现?

我以为会这样,

BandDetailViewController *bandDetailViewController = [[BandDetailViewController alloc] initWithNibName:@"BandDetailViewController" bundle:nil];
bandDetailViewController.band = [thisDay objectAtIndex:indexPath.row];

[super.navigationController pushViewController:bandDetailViewController animated:YES];
[bandDetailViewController release];

但是那行不通。

I have a view which is a part of a tabBarController. In this view I have a subview with a table. When clicking a cell in this table, I would like to access the navigationController of the parent view. Is this possible - and if so, how?

I thought it would be

BandDetailViewController *bandDetailViewController = [[BandDetailViewController alloc] initWithNibName:@"BandDetailViewController" bundle:nil];
bandDetailViewController.band = [thisDay objectAtIndex:indexPath.row];

[super.navigationController pushViewController:bandDetailViewController animated:YES];
[bandDetailViewController release];

But that does not work.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

风筝在阴天搁浅。 2024-10-25 01:50:15

当您实例化子视图控制器时,传入对导航控制器(超级控制器)的引用,并将其存储在实例变量中。然后您可以在需要时在子中引用它。我一直在寻找更优雅的解决方案来解决这个问题和类似的问题,但没有成功。传递参考作品,所以我这样做并尝试继续我的生活。

编辑:添加代码示例

在mainVC.m中

 // this might be in didSelectRowForIndexPath:
 SubViewController *subVC = [[SubViewController alloc] init];
 subVC.superNavController = self.navController;
 [self.navigationController pushViewController:subVC animated:YES];

在SubViewController.h中

 @property (nonatomic, retain) UINavigationController *superNavController;

在SubViewController.m中

  @synthesize superNavController;


  // then, wherever you need it, say to pass to a sub-sub-viewController...
  [self.superNavController pushViewController:myNewVC animated:YES];

When you instantiate your sub-view controller, pass in a reference to the navigation controller (super's), and store it in an instance variable. You can then reference it when you need it in the sub. I have been looking for a more elegant solution to this and similar problems, without success. Passing in a reference works, so I do that and try to get on with my life.

EDIT: add code sample

In mainVC.m

 // this might be in didSelectRowForIndexPath:
 SubViewController *subVC = [[SubViewController alloc] init];
 subVC.superNavController = self.navController;
 [self.navigationController pushViewController:subVC animated:YES];

In SubViewController.h

 @property (nonatomic, retain) UINavigationController *superNavController;

In SubViewController.m

  @synthesize superNavController;


  // then, wherever you need it, say to pass to a sub-sub-viewController...
  [self.superNavController pushViewController:myNewVC animated:YES];
深陷 2024-10-25 01:50:15

当你调用-pushViewController时,哪个视图控制器是self?如果您从选项卡子视图之一中调用它,则 self 可能没有从您添加到的顶级视图中对导航控制器的引用。

请看一下这篇文章。这家伙看起来也有同样的问题。

无法为子视图推送ViewController

When you call -pushViewController, which view controller is self? If you are calling that from within one of your tab subviews, self likely doesn't have a reference to the navigation controller from the top level view that you added it to.

Please have a look at this post. This guy looks like having same problem.

Unable to pushViewController for subview

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