-1

I am trying to pass Array from one UIViewController to other viewcontroller. I tried using property (viewcontroller1.array). I tried notifications (posting notification and adding observer ). I tried NSUserDefaults all are returning a null array.

Imad Ali
  • 3,261
  • 1
  • 25
  • 33
sampath
  • 41
  • 8

2 Answers2

1

in ViewController 1 from array is to passed

@interface demopreseViewController () {
    NSArray* nameArr;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    nameArr = [NSArray arrayWithObjects: @"Jill Valentine", @"Peter Griffin",nil];
    NSLog(@"%@",nameArr);

}

- (IBAction)PresentAction:(id)sender {
    ViewController2 *aviewcontroller = [self.storyboard instantiateViewControllerWithIdentifier:@"aa"];
    aviewcontroller.array1 = nameArr;
    [self presentViewController:aviewcontroller animated:YES completion:nil];
}

In viewController 2 Where array is to be passed

under ViewController2.h

@property (strong, nonatomic) NSArray *array1;

under ViewController2.m

- (void)viewDidLoad {
    [super viewDidLoad];
    NSLog(@"parsed%@",_array1);
}

Array Data will be passed

iOS Geek
  • 4,825
  • 1
  • 9
  • 30
  • .I tried the above type steps already but still null array iam getting. is it mandatory to give like this " ViewController2 *aviewcontroller = [self.storyboard instantiateViewControllerWithIdentifier:@"aa"];". ? when I gave the above line iam getting unrecognized selector sent to instance error. ofcourse I had given the identifier with the name i specified to my storyboard.I have many storyboard files in the project? does it make difference if I give self.storyboard? – sampath Jun 19 '17 at 04:53
  • Did you tried using identifier of storyboard like Below UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle: nil]; – iOS Geek Jun 19 '17 at 05:02
  • @sampath, `viewConrtoller2` is the name of view controller's class so you need to give yours. – vaibhav Jun 19 '17 at 05:05
  • I have tried below code and it worked. **MViewController *nextVC =(MViewController *)[self.storyboard instantiateViewControllerWithIdentifier:@"Service"]; ** – sampath Jun 19 '17 at 05:40
0

Define the property for Your Arrays

@Property(nonatomic, retain) NSArray *arrayName;

Secondly

ViewControllerClassName *controller = [self.storyboard instantiateViewControllerWithIdentifier:@"StoryboardIdentifier"];
controller.array = arrayName;
Raza.najam
  • 769
  • 6
  • 15