I trying to pass an array through prepareWithSegue but I get null when I launch the app
this is the code :
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.destinationViewController isEqual:@"table"]) {
PersonsViewController *person= [[PersonsViewController alloc]init];
[person setAnArray:anArray];
person = segue.destinationViewController;
}
}
and this is setAnArray method:
-(void)setAnArray:(NSMutableArray *)anArray
{
array = [[NSMutableArray alloc]initWithArray:anArray];
if (array != nil) {
NSLog(@"array is copied !!");
}
}
the data should pass from viewController(embedded with UINavigation Controller) to PersonViewController (which is tableview) and nothing shows on the table so I NSLogged the the array count and found it zero so I did some further checking with this code:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
#warning Incomplete method implementation.
// Return the number of rows in the section.
if (array == nil) {
NSLog(@"array is null");
}
else
{
NSLog(@"array count is %lu",(unsigned long)[array count]);
return [array count];
}
and I get array is null message.
please help me to fix this
Why don't you just allocate the view controller from the storyboard and pass the array in as a property of that view controller before you add it to the stack? i.e. avoid using prepareForSegue
-(void) buttonPressed:(UIButton*) sender
{
UIStoryBoard *story = [UIStoryboard storyboardWithName:@"Storyboard name"];
YourViewController *vc = [story instantiateViewControllerWithIdentifier:@"identifier"];
vc.array = <you array>
[self.navigationController pushViewController:vc animated:YES];
}
when you assign segue.destinationViewController to person your over writing the person object you previously instantiated and assigned the array to.
you probably want to do something like this
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.destinationViewController isEqual:@"table"]) {
[(PersonsViewController *) segue.destinationViewController setAnArray:anArray];
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With