How to Copy Array Objects which are in Different Tabs

◇◆丶佛笑我妖孽 提交于 2020-01-06 08:44:11

问题


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:

  1. 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
  2. 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.
  3. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!