开发者

Send NSMutableArray to a UIViewController

开发者 https://www.devze.com 2023-03-17 12:15 出处:网络
I am would like some help because I am trying to find a good way to post an NSMutableArray to another class.

I am would like some help because I am trying to find a good way to post an NSMutableArray to another class.

So I have a UIViewController with an NSMutableArray (Which is pushed if the user selects a cell from the RootViewController)

e.g.

Code in RootViewController when user selects a cell:

QueryTableViewController *queryTableView 
        = [[QueryTableViewController alloc] initWithQuery:query];
[self.navigationController pushViewController:queryTableView animated:YES];
[queryTableView release];

Code in QueryTableViewController

@interface QueryTableViewController : UIViewController{
NSMutableArray *results;
}

@implementation QueryTableViewController // more code
-(int)fetchQueryWithString:(NSString*)string{
    // Searching twitter.
    // add objects in the results array.
    // **HERE** Once I gather all the objects I am posting a notification:
}

On the other side I have the DetailViewController which is empty and needs the array to create another UIViewController and add the subview. e.g.

 // when the notification is received  
 self.statsTableView = [[StatsTableVC alloc] initWithTweets:**HERE**];   
 [self addSubView:self.statsTableView];

At the moment I am posting a notification开发者_JS百科 at the end of the fetchQueryWithString method to share the array. I would like to know if there is a better way to send it across to another UIViewController?

(Which I am sure there is ;)

Thanks in Advance.


There are two methods for handling data between multiple view controllers.

First, there is dependency injection which is simply passing a reference to a data storing object between the controllers. Since objects are passed by reference, you can modify the data object in one controller hand have the data appear in another.

In your case, your data object is just a mutable array. You would define a property to hold the array in every view controller that will use it. So, in your RootViewController you would define a NSMutableArray property, create an empty array and then pass it to the next view controller when you load.

QueryTableViewController *queryTableView 
        = [[QueryTableViewController alloc] initWithQuery:query];
queryTableView.results=self.queryResults;
[self.navigationController pushViewController:queryTableView animated:YES];
[queryTableView release];

Since the RootViewController instance and the QueryTableViewController instance share a reference to the same mutable array, each as access to the changes made by the other.

Dependency injection has the virtue of up front simplicity. However, it has the disadvantage of tying all the view controller's access to data into a rigid hierarchy. If you move the views controllers around in the view hierarchy, add or remove view controllers or move the view controller to another app, you may have to rewrite the data handling for all views.

The second method uses a pseudo-singleton to make the data object accessible anywhere in the app. The application is a singleton so that makes the app delegate effectively a singleton.

To get the application delegate anywhere in the app:

AppDelegateClassName *appDelegate=(AppDelegateClassName *)[[NSApplication sharedApplication]  delegate]; 

... then, to refer to any property of the app delegate:

NSMutableArray *localResults=appDelegate.results;

The advantage of this system is that it makes view controllers independent of one another. You can easily move them around or even move them to another app.


you could make your array available through singleton class if it's sharable across multiple classes in your application.

So, you singleton class will hold NSMutableArray instance and make it available to others by some getter function.


The following steps use the array in on class, B, which was created in another class,A.

In Class A's implementation file , above @implementation, declare an array like so:

NSArray *array;
@implementation

...in your method where you filled your NSMutableArray assign the mutable array to this array like:

array = arrMutable;

Now, in your class B above @implementation extern it like

extern NSArray *array;
@implementation

Now you have alll the elements in your array which was in nsmutable array

Hope it helps...........


please import appdelegate .h file in which class you want to use nsmutuable array. then property define in nsmutable array in appdelegate class.

then which class you want to use array.

    [yourappdelegateclass sharedinstance].appdelegatearray = yourarray;
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号