有更好的办法吗?代表->代表->代表

发布于 2024-10-21 22:10:54 字数 123 浏览 1 评论 0原文

我有一个加载视图的视图,需要将视图推送到主导航控制器。

我已经为每个视图设置了一个委托,并且基本上使我的调用返回到主导航控制器的“链”。

它有效,但我很好奇是否有更好(更简单?)的方法来实现这一目标?

I have a view that loads a view, that needs to push a view to the main navigation controller.

I've setup a delegate for each view, and basically make my call back up the 'chain' to the main navigation controller.

It works, but I am curious if there is a better (easier?) way to achieve this?

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

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

发布评论

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

评论(3

淡淡の花香 2024-10-28 22:10:54

您可以使用 NSNotificationCenter< /a> 发送您的 NavigationController 将响应的消息。

在需要调用 NavigationController 的视图中,您可以编写如下内容:

[[NSNotificationCenter defaultCenter] postNotificationName:@"DoWork" object:nil];

其中 @"DoWork" 是另一个对象将响应的唯一(最有可能)消息名称。

在您的 NavigationController 中,您需要添加一个观察者才能捕获该通知,如下所示:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(doWork) name:@"DoWork" object:nil];

其中 @selector(doWork) 是您在发布通知时要执行的选择器,并且 < code>@"DoWork" 是您要响应的通知。

You could use the NSNotificationCenter to send a message that your NavigationController will respond to.

In your view that needs to call the NavigationController, you would write something like:

[[NSNotificationCenter defaultCenter] postNotificationName:@"DoWork" object:nil];

Where @"DoWork" is the unique (most likely) message name to which another object will respond.

And in your NavigationController you would need to add an observer to be able to catch that notification, like so:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(doWork) name:@"DoWork" object:nil];

Where @selector(doWork) is the selector you want to perform when the notification is posted, and @"DoWork" is the notification you want to respond to.

冷情妓 2024-10-28 22:10:54

因此视图 A 加载视图 B。稍后,视图 B 加载视图 C 并希望将其视图控制器推送到视图 B 上游的导航控制器上?查看 A 的?视图控制器。

假设视图 B 有一个属于导航堆栈一部分的视图控制器,那么它的视图控制器就可以通过 [self navigationController] 获取导航控制器,无论它在堆栈中有多远。

视图没有指向其控制器的指针,但如果您想完全破坏 MVC,您可以让控制器自行设置一个指针。更好的方法是让控制器接收点击操作(或者提示加载和推送视图 C 的任何内容),然后让视图控制器处理交换。例如,如果视图 B 是表视图,您可以将其视图控制器设置为 UITableViewDelegate,以便它可以通过推送新视图控制器来处理行选择。

您的问题似乎表明您没有区分视图、其视图控制器、导航控制器以及导航控制器内容区域中显示的视图。所有这些对象都扮演着不同的角色,因此在继续开发您的应用程序之前,更仔细地观察它们及其相互关系会对您有所帮助。

So view A loads view B. Later, view B loads view C and wants to push its view controller onto the navigation controller upstream from view B's? view A's? view controller.

Provided view B has a view controller that is part of the navigation stack, then its view controller can grab the navigation controller, no matter how far down the stack it is, via [self navigationController].

Views do not have a pointer to their controller, but if you wanted to totally break MVC, you could have the controller set one itself. The better approach would be to have the controller receive the tap action (or whatever it is that prompts loading and pushing view C) and then have the view controller handle the swapping. For example, if view B is a table view, you would set its view controller as the UITableViewDelegate so that it can handle selection of a row by pushing a new view controller.

Your question seems to indicate that you are not distinguishing between a view, its view controller, a navigation controller, and the view that's being displayed in the navigation controller's content region. All these objects play a different rôle, so it would pay off for you to look more closely at them and their interrelationships before continuing to develop your application.

花落人断肠 2024-10-28 22:10:54

根据您设置程序的方式,如果您的应用程序委托包含 UINavigationController,您可以从任何地方访问导航控制器:

[[[NSApp delegate] navigationController] pushViewController: myVC animated:YES];

但同样,只有当您的应用程序委托包含 UINavigationController 时才有效

Depending on how you've set up your program, if your App Delegate contains a UINavigationController you can access the navigation controller from anywhere:

[[[NSApp delegate] navigationController] pushViewController: myVC animated:YES];

But again, that only works if your app delegate contains a UINavigationController.

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