0

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);
   }
}
6
  • 1
    Weak or Strong pointers? Commented Nov 22, 2013 at 13:21
  • 2
    Can we see textArraysParsed's declaration? Commented Nov 22, 2013 at 13:21
  • is it a property? strong reference? Commented Nov 22, 2013 at 13:23
  • is destViewController nil? Commented Nov 22, 2013 at 13:25
  • you might be allocating the array in the synthesized class,this may be one reason.You need to remove it if you are doing. Commented Nov 22, 2013 at 14:11

4 Answers 4

2

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];
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

Just modify the property method:-

  @property (nonatomic, strong)NSMutableArray *textArrays;

Comments

0

All I had to do is change strong to retain.

2 Comments

So you weren't sure of the difference between ARC and MRR? If that's the case then this issue is the tip of the iceberg.
Did you solve your problem? Please mark the correct answer to mark this post as answered.
-2

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

4 Comments

Using the defaults system to pass an object from one view controller to another seems a bit complicated when you could as easily assign a value to a property in the target controller.
I can't agree with this pattern. UserDefaults are for storing permanent information, just because it can be used to pass information around globally doesn't mean it should.
Code I posted here is valid. Just because there are a 100 ways to skin a cat doesn't mean this way is any less wrong.
But I want to implore readers not to use this answer. I stand by my downvote.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.