如何正确地将 NSArray / NSMutableArray 从一个 UIViewController 传递到另一个 UIViewController

发布于 2024-08-29 05:47:04 字数 188 浏览 1 评论 0原文

我正在开发的一个 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 技术交流群。

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

发布评论

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

评论(2

长不大的小祸害 2024-09-05 05:47:04

一般来说,有两种方法。

在第一种方法中,当您实例化下一个视图控制器时,您可以将数组属性设置为从当前(即将成为上一个或父级)视图控制器“继承”:

MyNextViewController *_myNextViewController = [[MyNextViewController alloc] initWithNibName:@"MyNextViewController" bundle:nil];
_myNextViewController.myViewControllerArray = self.myViewControllerArray;
...

在第二种方法中,您可以创建一个 myAppDelegateArray 应用程序委托中的属性,在应用程序初始化时实例化数组(或根据需要在其他地方实例化)。

然后,您可以从任何类(包括视图控制器)调用此属性的 getter。

例如,在 MyNextViewController 的实例中,您可能有一个名为 myViewControllerArray 的属性,并且您可以按如下方式设置它:

self.myViewControllerArray = [UIAppDelegate myAppDelegateArray]; 

您可以添加一个 #define 在应用程序常量文件中的某个位置声明,例如:

#define UIAppDelegate ((MyAppDelegate *)[UIApplication sharedApplication].delegate)

或者如果您愿意,可以使用完整的 [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:

MyNextViewController *_myNextViewController = [[MyNextViewController alloc] initWithNibName:@"MyNextViewController" bundle:nil];
_myNextViewController.myViewControllerArray = self.myViewControllerArray;
...

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 called myViewControllerArray and you would set it as follows:

self.myViewControllerArray = [UIAppDelegate myAppDelegateArray]; 

You would add a #define statement somewhere in your application constants file, e.g.:

#define UIAppDelegate ((MyAppDelegate *)[UIApplication sharedApplication].delegate)

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.

谁把谁当真 2024-09-05 05:47:04

您可能没有保留它,它被释放,并且您正在尝试再次访问它。尝试在使用它的地方(在每个控制器中)发送 [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

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