Pass NSMutableArray between two UIViewController's

被刻印的时光 ゝ 提交于 2019-12-23 05:15:26

问题


I have two view controllers name RootViewController and SecondViewController. In the FirstViewController I have this NSMutableArray

@interface RootViewController : UITableViewController {

NSMutableArray *allClasses;}@property (nonatomic,retain) NSMutableArray *allClasses;

In the RootViewController I populate the UITableView with all the objects within allClasses

In my SecondViewController I have

@interface SecondViewController : UIViewController <UITextFieldDelegate,UIPickerViewDelegate> {

NSMutableArray *arrayStrings;}

I have a method that adds new NSStrings to the arrayStrings. My goal is to be able to pass the arrayStrings to the RootViewController by trying something similar to allClasses = arrayStrings. That way when the RootViewController is loaded it can populate with new information.

How would I got about accomplishing that task?


回答1:


in RootViewController implement

-(void)viewWillAppear:(BOOL)animated

this delegate method. Inside this method

write

self.allClasses = SecondViewControllerObj.arrayStrings;

SecondViewControllerObj is the object of SecondViewController declared in RootViewController as a member and which is used to navigate to that view

like

if(SecondViewControllerObj == nil)
    {
        SecondViewControllerObj = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
    }

        [self.navigationController pushViewController:SecondViewControllerObj animated:YES];



回答2:


You need to have the reference of root view controller in the second view controller. And there you need to set the allclasses to arrayStrings; In general you can find reference to any view controller pushed into the stack by the following code.

NSArray *viewConts = [[self navigationController] viewControllers];
        for(int i=0;i<[viewConts count];i++)
        {
            if([[viewConts objectAtIndex:i] isKindOfClass:[RootViewController class]]){
                RootViewController *rootController = (RootViewController *)[viewConts objectAtIndex:i];
                [rootController setAllClasses:arrayStrings];

            }

        }

Now in the viewWillAppear of RootViewController you need to reload the contents of your view.

Hope this helps.

Thanks,

Madhup



来源:https://stackoverflow.com/questions/2635215/pass-nsmutablearray-between-two-uiviewcontrollers

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