问题
I have NSMutable Array in FirstviewController. I want to copy objects of that array to another NSMUtable Array which is in SecondViewController. But FirstViewController and SecondViewController are in different tabs (my app's rootController is a tabBarController )
Note: I know It's work for [self.navigationController pushViewController:firstViewController animated:YES]; But, how can I do it when viewControllers are in different tabs like in my case.
please give me a help. thanks in advanced.
回答1:
There are a few ways to do this type of pattern:
- Delegation. This is where you would make one view controller know of the other by passing a pointer to it. So you create controller 1 first somewhere, then after controller 1 is created, you tell controller 2 about the controller 1 so that controller 2 can access controller 1's public instance variables
- Notifications. You can send object through a notification system that you would register to listen to. Here controller 2 would listen for a certain notifications that controller 1 would send when you need it to.
- Global variables. This is where you store the array abject not in each view controller, but in a global place accessible to all such as the App Delegate or the root view controller.
There are more design patterns, but these are your best bet for this type of setup. For more info on these, check the Cocoa Fundamentals Guide provided by Apple
回答2:
In below code drink
is array of detailViewController
class and drinks
is array of current
class from where i want to navigate to another class and want to initialize the array on other class...
DetailViewController *detailViewController = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil];
detailViewController.drink = [self.drinks];
[self.navigationController pushViewController:drinkDetailViewController animated:YES];
[detailViewController release];
来源:https://stackoverflow.com/questions/7215450/how-to-copy-array-objects-which-are-in-different-tabs