简体   繁体   中英

Array not passing between view controllers ios

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);
   }
}

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

只需修改属性方法: -

  @property (nonatomic, strong)NSMutableArray *textArrays;

我所要做的就是改变强势以保留。

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM