以正确的方式识别 UITabBarItems 视图控制器实例

发布于 2024-11-30 18:10:51 字数 1458 浏览 2 评论 0原文

我有一个带有选项卡栏的应用程序来打开不同的视图控制器,如下所示:

firstViewController = [[UITableViewController alloc] init];
UINavigationController *firstNavigationController = [[UINavigationController alloc] initWithRootViewController:firstViewController];
[firstNavigationController setTabBarItem:[[UITabBarItem alloc] initWithTitle:@"Add" image:[UIImage imageNamed:@"Add.png"] tag:1]];

[viewControllers addObject:firstNavigationController];

secondViewController = [[UITableViewController alloc] init];
UINavigationController *secondNavigationController = [[UINavigationController alloc] initWithRootViewController:secondViewController];
[secondNavigationController setTabBarItem:[[UITabBarItem alloc] initWithTitle:@"List" image:[UIImage imageNamed:@"List.png"] tag:2]];

[viewControllers addObject:secondNavigationController];

UITabBarController *tabBarController = [[UITabBarController alloc] init];

[tabBarController setViewControllers:viewControllers];

[[self window] setRootViewController:tabBarController];

这很好。现在我有一个额外的导航需求,firstViewController 可能会从 SecondViewController 调用,并将数据传递给它。

我发现将数据传递到由选项卡栏访问的firstViewController的同一实例的唯一方法如下(在secondViewController代码中):

firstViewController = [[[[[self tabBarController] viewControllers] objectAtIndex:0] viewControllers] objectAtIndex:0];

这工作正常,但我发现它很混乱,特别是如果我决定更改选项卡栏控制器中视图的顺序。

我也探索过标签方式,但似乎并没有对代码进行太多改进。

还有另一种更干净的方法吗?

I have an application with a Tab Bar to open different view controllers like this:

firstViewController = [[UITableViewController alloc] init];
UINavigationController *firstNavigationController = [[UINavigationController alloc] initWithRootViewController:firstViewController];
[firstNavigationController setTabBarItem:[[UITabBarItem alloc] initWithTitle:@"Add" image:[UIImage imageNamed:@"Add.png"] tag:1]];

[viewControllers addObject:firstNavigationController];

secondViewController = [[UITableViewController alloc] init];
UINavigationController *secondNavigationController = [[UINavigationController alloc] initWithRootViewController:secondViewController];
[secondNavigationController setTabBarItem:[[UITabBarItem alloc] initWithTitle:@"List" image:[UIImage imageNamed:@"List.png"] tag:2]];

[viewControllers addObject:secondNavigationController];

UITabBarController *tabBarController = [[UITabBarController alloc] init];

[tabBarController setViewControllers:viewControllers];

[[self window] setRootViewController:tabBarController];

This is fine. Now I have an additional navigation requirement, the firstViewController might be called from the secondViewController, passing data to it.

The only way I have found to pass data to the very same instance of firstViewController which is accessed by the tab bar is the following (in the secondViewController code):

firstViewController = [[[[[self tabBarController] viewControllers] objectAtIndex:0] viewControllers] objectAtIndex:0];

This works fine, but I find it messy, particularly if I decide to change the order of the views in the tab bar controller.

I have also explored the tag way, but didn't seem to improve the code that much.

Is there another, cleaner, way?

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

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

发布评论

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

评论(1

死开点丶别碍眼 2024-12-07 18:10:51

我不能声称知道您想通过将数据从一个视图控制器发送到另一个视图控制器来完成什么,但如果发送数据是您的唯一目的,那么我会推荐 单例模式。或者我会推荐 NSNotificationCenter< /a>.单例可以保存可以从任何地方检索的应用程序范围的数据。您甚至可以(尽管这违反最佳实践)在单例类中保留对视图控制器的引用。我还大量使用通知来传递数据。只需发布通知并让其他班级收听即可。

例如,在 viewDidLoad 中的 firstViewController 中,您可以这样做:

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

然后在 secondaryViewController 中,当您想传递数据时,您可以发布此(
其中“myData”是一个数据对象(如 NSDictionary)):

[[NSNotificationCenter defaultCenter] postNotificationName:myNotificationName object:nil userInfo: myData];

然后在firstViewController 中此方法将接收数据。

-(void)theMethodToCall:(NSNotification *)notif
 {
      NSDictionary *dict = [notification userInfo];
 }

通过此代码,您还可以获得不错的对象引用

I can't claim to know what you are trying to accomplish by sending data from one viewcontroller to another, but if sending data is your only purpose then I would recommend the Singleton Pattern. Or I would recommend NSNotificationCenter. A singleton could hold app wide data that can be retrieved from anywhere. You could even (though its against best practices) hold references to your viewcontrollers in the singleton class. I also heavily use notifications to pass around data. Simply post a notification and have another class listen for it.

For instance in firstViewController in viewDidLoad you could do this:

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

Then in secondViewController, when you want to pass data you could post this(
Where "myData" is an data object (Like NSDictionary)):

[[NSNotificationCenter defaultCenter] postNotificationName:myNotificationName object:nil userInfo: myData];

Then in firstViewController this method would receive the data.

-(void)theMethodToCall:(NSNotification *)notif
 {
      NSDictionary *dict = [notification userInfo];
 }

With this code you also get the nice object reference

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