管理 NSNotification 以及 UITabBarController 交互

发布于 2024-09-15 15:16:21 字数 1053 浏览 3 评论 0原文

我有 1 个主 UIViewController,其中包含一个 UITabBarController。标签栏控制器有 4 个 UIViewController(每个由 UINavigationController 管理)。简而言之,它看起来像这样:

MainViewController
   |
   |--- FirstUIViewController
   |
   |--- SecondUIViewController
   |
   |--- ThirdUIViewController
   |
   |--- FourthUIViewController

默认情况下加载 FirstUIViewController。

在 SecondUIViewController 中,可能会发生一个事件。该事件具有随之传递的特定数据。当此事件发生时,我希望发生两件事:

1)在选项卡栏控制器中选择 FourthUIViewController 以直观地显示已输入不同的选项卡。

2) FourthUIViewController 中的后续操作,其中选择特定的 UITableViewCell。所选单元格由事件中传递的数据确定。

为了做到这一点,我在 MainViewController 中创建了一个 NSNotification 观察者。它侦听来自 SecondUIViewController 的事件并通过 userInfo 从中获取数据。

我还知道,从 MainViewController 中,我可以轻松地在 UITabBarController 上设置 selectedIndex,以便显示正确的选项卡。 (#1 已完成)。

然而,实现#2 让我感到困惑。我不知道如何(正确地、正确地)设置选项卡栏的 selectedIndex ,然后让 FourthUiViewController 作用于选定的单元格。我的假设是 FourthUIViewController 在此事件发生之前可能尚未加载,因此我无法设置任何参数或调用任何观察者。

也许更广泛的问题是,通过选项卡选择加载 UIViewController 时如何传递数据(与推送新的 UIViewController 相比,这非常简单)。

TIA。

I have 1 main UIViewController that contains a UITabBarController. The tab bar controller has 4 UIViewControllers (each managed by UINavigationControllers). Succinctly, it looks like this:

MainViewController
   |
   |--- FirstUIViewController
   |
   |--- SecondUIViewController
   |
   |--- ThirdUIViewController
   |
   |--- FourthUIViewController

The FirstUIViewController is loaded by default.

In the SecondUIViewController, there is an event that can occur. This event has specific data that is passed along with it. When this event occurs I want 2 things to happen:

1) FourthUIViewController to be selected in the tab bar controller to visually show a different tab has been entered.

2) A subsequent action in FourthUIViewController where a specific UITableViewCell is selected. The selected cell is determined by the data that is passed along in the event.

In order to do this, I've created an NSNotification observer in the MainViewController. It listens for the event from the SecondUIViewController and gets data from it via userInfo.

I also know that from MainViewController I can easily set the selectedIndex on the UITabBarController in order to have the correct tab shown. (#1 accomplished).

However, accomplishing #2 baffles me. I don't know how to (properly, correctly) set the selectedIndex of the tab bar and then have the FourthUiViewController act upon a selected cell. My assumption is that FourthUIViewController may not have been loaded before this event occurs so I couldn't set any parameters or call any observers.

Perhaps the broader question is how data can be passed when loading a UIViewController through tab selection (compared to pushing a new UIViewController which is very straightforward).

TIA.

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

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

发布评论

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

评论(2

不打扰别人 2024-09-22 15:16:21

视图控制器将始终在启动时加载,除非您对 NIB 文件做了一些非常奇怪的事情。

控制器的视图可能尚未加载,但您可以通过调用[fourthController view](或者,如果合适的话,[ self view]),然后再操作该视图的表。

The view controller will always be loaded at startup, unless you've done something very weird with your NIB file.

It's possible that the controller's view won't have been loaded, but you can force that to happen by calling [fourthController view] (or, if appropriate, [self view]) before you manipulate that view's table.

心房敞 2024-09-22 15:16:21

如果需要,通知可以有多个收件人。为什么不让您的其他视图简单地监听相同的通知并根据 UserInfo 选择所需的表。

我理解对了吗?

If needed, there can be many recipients to a notification. Why not have your other view simply listen to the same notification and select the desired table based on the UserInfo.

Did I understand that right?

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