在 UITabBarController xcode 中的 UIViewController 和 UIViewController 之间共享数据
我知道如何在两个视图之间共享数据。但如果我想使用 tabBarController 共享数据,我就迷失了。
这是我要移动到 tabBar 的 IBAction。
-(IBAction)goToPage2:(id)sender
{
tabController.modalTransitionStyle=UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:tabController animated:YES];
}
我需要在 tabBar 的第一个视图中的 IBAction 中共享我的 NSString *dataStr。
firstView *first = [[firstView alloc] initWithNibName:@"firstView" bundle:nil];
first.dataStr = name.text;
[tabController presentModalViewController:first animated:YES];
这段代码不起作用。
谢谢
I know how to do for share data between 2 views. But if I want to share data using a tabBarController I'm lost.
This is my IBAction to move to my tabBar.
-(IBAction)goToPage2:(id)sender
{
tabController.modalTransitionStyle=UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:tabController animated:YES];
}
i need to share my NSString *dataStr in my IBAction in the first view of my tabBar.
firstView *first = [[firstView alloc] initWithNibName:@"firstView" bundle:nil];
first.dataStr = name.text;
[tabController presentModalViewController:first animated:YES];
this code doesn't work.
thx
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在您的应用程序委托中声明 @property。您可以从应用程序的任何位置访问您的应用程序委托。
Declare a @property in your app delegate. And you can access your app delegate from any point of your app.
我同意特伦特的建议。但我建议您在这种情况下应该使用数据类。在 Appdelegate 中使用属性并不是一个好的做法。为此,您应该始终使用数据类。
您可以按如下方式创建数据类:
您需要创建一个 Data 类,您可以在其中设置变量或案例数组的属性(用于在 UITableView 中显示数据)。在数据类中实现一个类方法,用于检查该对象是否已实例化。如果没有,它就会这样做。它是这样的:
//DataClass.h
//DataClass.m
现在,在视图控制器中,您需要调用此方法:
并使用数组。
这样你就可以在不干扰AppDelegate的情况下分配数据,这是一个很好的做法。
I agree with what Terente suggested. But I recommend you should use a Data Class in this case. Using a property in Appdelegate is not a good practice. You should always use Data Class for this.
You create a Data class as follows:
You need to create a Data class where you can set the properties of variables or in your case arrays (for displaying data in UITableView). Implement a class method in data class which checks that object has been instantiated or not. If not, it does that. It is something like this :
//DataClass.h
//DataClass.m
Now in your view controller you need to call this method as :
And use the arrays.
This way you can assign data without disturbing AppDelegate, which is a good practice.
我在博客上写了一篇关于此类问题的冗长教程:http://www.hollance.com/2011/04/making-your-classes-talk-to-each-other-part-1/
你需要找到一个干净的方法让您的控制器与每个控制器进行通信 其他。我的教程解释了执行此操作的几种方法以及每种方法的优点和缺点。值得学习如何执行此操作,因为几乎您将编写的所有应用程序都会出现此问题。
I wrote a lengthy tutorial about such issues on my blog: http://www.hollance.com/2011/04/making-your-classes-talk-to-each-other-part-1/
You need to figure out a clean way to let your controllers communicate with each other. My tutorial explains several ways to do this and what the advantages and downsides are of each approach. It's worth learning how to do this, because this issue comes up in almost any app you will write.