如何正确地将 NSArray / NSMutableArray 从一个 UIViewController 传递到另一个 UIViewController
我正在开发的一个 iPhone 应用程序会定期终止......我有一种感觉,这是因为我在多个视图控制器之间访问共享 NSArray 的方式。那么...
在多个 ViewController 之间传递 NSArray/NSMutableArray 的最佳方法是什么...所有这些都可以修改和/或将其设置为 nil 或其他什么?
谢谢
An iphone app I'm working on is terminating periodically ... and I have a feeling it is because of how I'm accessing a shared NSArray amongst several view controllers. So ...
What is the best way to pass NSArray/NSMutableArray amongst several ViewControllers ... all of which can modify and/or set it to nil or whatever?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一般来说,有两种方法。
在第一种方法中,当您实例化下一个视图控制器时,您可以将数组属性设置为从当前(即将成为上一个或父级)视图控制器“继承”:
在第二种方法中,您可以创建一个
myAppDelegateArray
应用程序委托中的属性,在应用程序初始化时实例化数组(或根据需要在其他地方实例化)。然后,您可以从任何类(包括视图控制器)调用此属性的 getter。
例如,在
MyNextViewController
的实例中,您可能有一个名为myViewControllerArray
的属性,并且您可以按如下方式设置它:您可以添加一个
#define
在应用程序常量文件中的某个位置声明,例如:或者如果您愿意,可以使用完整的
[UIApplication sharedApplication].delegate
调用。作为一般方法,人们似乎更喜欢应用程序委托方法,因为它分散了对所需属性的访问。
使用这种方法,如果您重新排列视图控制器或将新的视图控制器插入到 VC 层次结构中,则无需调试从父视图控制器继承的子视图控制器属性,因为引用始终可从应用程序委托获得。
There are two approaches, generally speaking.
In the first approach, when you instantiate your next view controller, you could set an array property to "inherit" from the current (soon-to-be previous or parent) view controller:
In the second approach, you could create a
myAppDelegateArray
property in your application delegate, instantiating the array when the application initializes (or instantiated somewhere else, on demand).You can then call the getter for this property from any class — including view controllers.
For example, within an instance of
MyNextViewController
, you might have a property calledmyViewControllerArray
and you would set it as follows:You would add a
#define
statement somewhere in your application constants file, e.g.:or use the full
[UIApplication sharedApplication].delegate
call, if you prefer.As a general approach, people seem to prefer the app delegate approach, as it decentralizes access to the desired property.
With this approach, if you rearrange your view controllers or insert new view controllers into your VC hierarchy, you wouldn't need to debug child view controller properties inherited from parent view controllers, because a reference is always available from the app delegate.
您可能没有保留它,它被释放,并且您正在尝试再次访问它。尝试在使用它的地方(在每个控制器中)发送 [myArray keep],并在使用完毕后发送 [myArray release]
you are probably not retaining it, it gets deallocated and you are trying to access it again. try to send a [myArray retain] where you are using it (in each of the controllers), and [myArray release] when you are done with it