我的多视图应用程序中可以有一个 NSMutableArray 吗?
我有一个基于导航的应用程序,它有多个视图。是否可以在整个应用程序中使用一个 NSMutableArray?我可以在一个视图中向该 NSMutableArray 添加对象,然后从其他视图中的同一 NSMutableArray 中删除对象吗?我尝试过,
myappAppDelegate *appDelegate = (myappAppDelegate *)[[UIApplication sharedApplication] delegate];
但当我尝试访问 appDelegate 的数组时,它给了我 null 。如果有人能给我任何想法或帮助链接或教程。提前致谢。
I have a navigational based application which has multiple views. Is it possible to use one single NSMutableArray for the whole applicaiton? Can i add objects to that NSMutableArray in one view and then remove object from the same NSMutableArray from some other view? I tried
myappAppDelegate *appDelegate = (myappAppDelegate *)[[UIApplication sharedApplication] delegate];
but it gives me null when i try to access appDelegate's array. If anyone can give me any idea or helping link or tutrorial. Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您的应用程序中有多个视图,并且在这种情况下您希望每个视图都可以访问一个变量,那么您应该始终创建一个 Model/Data(singleton) 类并在其中定义该变量。像这样的事情:
现在在您的视图控制器中,您需要将此方法调用为:
每个视图控制器都可以访问该变量。您只需创建 Data 类的实例。
If you are having multiple views in your application, and in that case you want to have a variable accessible to every view, you should always create a Model/Data(singleton) class and define the variable in it. Something like this :
Now in your view controller you need to call this method as :
This variable will be accessible to every view controller. You just have to create an instance of Data class.
对于你的问题类型,我会使用单例。
http://en.wikipedia.org/wiki/Singleton_pattern
appdelegate 也是一个单例,但你如果您使用自己的单例,可以减少一点编码行数。
For your type of issue I would use a singleton.
http://en.wikipedia.org/wiki/Singleton_pattern
The appdelegate is a singleton too but you can reduce a bit the number of coded lines if you use your own singleton.
AppDelegate 方法应该可以工作,并且您可能应该弄清楚为什么它不起作用,即使您使用的是单例。
获取
appDelegate
指针的语句似乎是正确的,因此我猜测指向数组的指针未在您的myappDelegate
类中设置(并保留) ,或者您一开始就没有正确创建 AppDelegate 实例。The AppDelegate approach should work, and you should probably figure out why it's not working, even if you go with a singleton.
The statement to get your
appDelegate
pointer appears to be correct, so I'm guessing that the pointer to the array is either not getting set (and retained) in yourmyappDelegate
class, or you did not create the AppDelegate instance correctly in the first place.在
Singleton
方法中添加以下内容:
这样您就可以初始化数组并正确使用它。
On the
Singleton
approach add thisthis way:
This way you can initilize the array and use it properly.