在另一个类中访问RootViewController的数据成员 [英] Accessing data member of RootViewController in another class

查看:125
本文介绍了在另一个类中访问RootViewController的数据成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Objective-C开发一个iPhone应用程序。我想访问另一个类中的RootViewController类的NSMutableArray类型的数据成员。我试着使数组是静态的。但我想有一个非静态数组。


b >
  • 引用 RootViewController

  • 将变量从 RootViewController

  • RootViewController 每个应用程序,在应用程序的委托中保持对该对象的引用是有意义的。您可以这样获得应用程式委托:

      AppDelegate * appDelegate = [[UIApplication sharedApplication] delegate]; 

    在您的应用委托类中,您应该添加一个名为 rootViewController ,暴露您的 RootViewController 对象。现在你可以写下像

      RootViewController * theRootViewController = appDelegate.rootViewController; 

    这满足第一个要求。现在,为了访问视图控制器所拥有的对象,您需要向 RootViewController 添加一个属性。你没有真正说出对象是什么,所以让我们调用它 myMutableArray 。在 RootViewController 上创建一个 readonly 属性,现在你可以这样写: / p>

      NSMutableArray * myArray = theRootViewController.myMutableArray; 

    这将让你做你想做的事。



    我必须警告你,但是:暴露一个 NSMutableArray 通常不是一个好主意。原因是如果你改变数组的内容, RootViewController 将不知道你这样做。所以如果你创建一个主细节视图,你的 RootViewController 将不知道你已经添加了一个新的对象。



    如果你写的方法让 RootViewController 在内部修改数组会更好。例如,你可以有一个名为 addFooObject:的方法来管理数组。然后视图控制器将知道你做了什么。对于访问,您可以很容易地从您的属性中返回一个不可变的,可自动释放的数组的副本。


    I am developing an iPhone application using Objective-C. I want to access a data member which is of type NSMutableArray of the RootViewController class in another class. I tried making the array static. But I would like to have a non static array. How do I achieve this?

    解决方案

    You need two things:

    1. a reference to the RootViewController
    2. a means of getting your variable out of RootViewController.

    In a situation where you have (most likely) one RootViewController per application, it makes sense to keep a reference to that object in your application's delegate. You can get the app delegate like this:

    AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
    

    In your app delegate class you should add a property called rootViewController that exposes your RootViewController object. Now you can write things like

    RootViewController *theRootViewController = appDelegate.rootViewController;
    

    This satisfies the first requirement. Now, in order to access the object owned by the view controller, you'll need to add a property to RootViewController. You didn't really say what the object is or does, so let's just call it myMutableArray. Create a readonly property with that name on RootViewController, and now you'll be able to write things like this:

    NSMutableArray *myArray = theRootViewController.myMutableArray;
    

    This will let you do what you want to do.

    I have to warn you, though: exposing an NSMutableArray is generally not a great idea. The reason is that if you change the contents of that array, RootViewController will have no idea that you've done this. So if you were creating, say, a master-detail view, your RootViewController would not know that you've added a new object.

    It would be better if you wrote methods that let RootViewController modify the array internally. For example, you could have a method named addFooObject: that manages the array. Then the view controller will know what you've done to it. For access, you could very easily return an immutable, autoreleased copy of the mutable array from your property.

    这篇关于在另一个类中访问RootViewController的数据成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆