Reputation: 985
I have an array that I want to pass between viewcontrollers. From ViewControllerA to ViewControllerB but my result comes out nil.I have tried just about everything.
When I log destViewController.textArraysParsed in ViewControllerA I see the correct result.
Array (
Blake
)
But when I try to NSLog the array in ViewControllerB the array is Null? I tried the viewDidLoad, viewWillAppear and viewWillLoad methods to try NSLog the array but it comes out nil.
How can I use this array that I made up in ViewControllerA so I can access the array in ViewControllerB.
ViewControllerA Methods
ViewControllerA.h
#import <UIKit/UIKit.h>
#import "BSKeyboardControls.h"
#import "RegistrationBaseViewController.h"
@interface Register1ViewController : RegistrationBaseViewController <UITextFieldDelegate, UITextViewDelegate, BSKeyboardControlsDelegate, UIPickerViewDelegate, UIActionSheetDelegate, UIPickerViewDataSource> {
NSMutableArray *textArrays;
}
@property (nonatomic, retain) NSMutableArray *textArrays;
@end
ViewControllerA.m
textArrays =[[NSMutableArray alloc]initWithCapacity:10];
NSString *arrayString = [NSString stringWithFormat:@"%@", self.firstNameTxtFld.text];
[textArrays addObject: arrayString];
NSLog(@"Array %@", textArrays);
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"segRegister2"]) {
Register2ViewController *destViewController = segue.destinationViewController;
destViewController.textArraysParsed = textArrays;
NSLog(@"destViewController Array %@", destViewController.textArraysParsed);
}
}
Upvotes: 0
Views: 3933
Reputation: 3477
iOS 7 - ARC
Here it didn't work with both "strong" nor "retain". (I let it "strong" in this case).
You have to initialise it first. It worked.
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.destinationViewController isKindOfClass:[Simulate class]]) {
if ([segue.identifier isEqualToString:@"SegueName"]) {
ViewController *vc = (ViewController *)segue.destinationViewController;
vc.array = [[NSMutableArray alloc] init];
vc.array insertObject:@"Obj" atIndex:0];
}
}
}
Upvotes: 2
Reputation: 14995
Just modify the property method:-
@property (nonatomic, strong)NSMutableArray *textArrays;
Upvotes: 1
Reputation: 27598
This is how I usually pass arrays between view controllers. I use NSUserDefaults, unless I miss understood your question.
In your ViewController A
NSMutableArray *temp2MutableArray = [[NSMutableArray alloc] initWithObjects:@"1", @"2", nil];
[[NSUserDefaults standardUserDefaults] setObject:temp2MutableArray forKey:@"mySampleArray"];
[[NSUserDefaults standardUserDefaults] synchronize];
In your ViewController B
NSArray *tempArray2 = [[NSUserDefaults standardUserDefaults] objectForKey:@"mySampleArray"];
NSMutableArray *temp2MutableArray = [[NSMutableArray alloc] initWithArray:tempArray2];
Upvotes: -2