开发者

Pass NSMutableArray between two UIViewController's

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 = arrayStrin开发者_运维知识库gs. That way when the RootViewController is loaded it can populate with new information.

How would I got about accomplishing that task?


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];


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

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜