以正确的方式识别 UITabBarItems 视图控制器实例
我有一个带有选项卡栏的应用程序来打开不同的视图控制器,如下所示:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不能声称知道您想通过将数据从一个视图控制器发送到另一个视图控制器来完成什么,但如果发送数据是您的唯一目的,那么我会推荐 单例模式。或者我会推荐 NSNotificationCenter< /a>.单例可以保存可以从任何地方检索的应用程序范围的数据。您甚至可以(尽管这违反最佳实践)在单例类中保留对视图控制器的引用。我还大量使用通知来传递数据。只需发布通知并让其他班级收听即可。
例如,在 viewDidLoad 中的 firstViewController 中,您可以这样做:
然后在 secondaryViewController 中,当您想传递数据时,您可以发布此(
其中“myData”是一个数据对象(如 NSDictionary)):
然后在firstViewController 中此方法将接收数据。
通过此代码,您还可以获得不错的对象引用
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:
Then in secondViewController, when you want to pass data you could post this(
Where "myData" is an data object (Like NSDictionary)):
Then in firstViewController this method would receive the data.
With this code you also get the nice object reference