简体   繁体   中英

Why is my variable null when I am attempting to access it from another view controller (Objective-c?

I am trying to use the .text of UITextField in my first view controller in another .text of UITextField in my second view controller, but my first view controller firstTField.text turns out to be (null) in my second view controller even though I printed _firstTField.text in my first view controller and it printed out the input that was entered.

What may be the problem? Why is null? Could someone guide me on how to approach/resolve this?

FirstViewController.h

@property (weak, nonatomic) IBOutlet UITextField *firstTField;

SecondViewController.h

@property (weak, nonatomic) IBOutlet UITextField *secondTField;

SecondViewController.m

#import "FirstViewController.h"

- (void)viewDidLoad {
     [super viewDidLoad];
     FirstViewController *firstPage = [[FirstViewController alloc] init];
     _secondTField.text = firstPage.firstTField.text;
}

I am going to assume that FirstViewController segues to SecondViewController . There are two things going on with your code that will prevent secondTField from having its text set:

1) You are instantiating a new instance of FirstViewController instead of referencing the one that you segued from. This new FirstViewController will not be in the view hierarchy and therefore will not have its text field set.

2) You will need to use an NSString during the segue to hold that information temporarily in SecondViewController while it gets loaded into the view hierarchy.

Your code would then look like this:

FirstViewController.h

@property (weak, nonatomic) IBOutlet UITextField *firstTField;

SecondViewController.h

@property (weak, nonatomic) IBOutlet UITextField *secondTField;
@property (strong, nonatomic) NSString *secondTFieldText;  // temporary to hold the text from FirstViewController

FirstViewController.m

#import "SecondViewController.h"

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    SecondViewController *secondViewController = (SecondViewController *)segue.destinationViewController;
    secondViewController.secondTFieldText = self.firstTField.text;
}

SecondViewController.m

#import "FirstViewController.h"

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.secondTField.text = self.secondTFieldText;
}

I hope this can help you

properties marked with IBOutlets are assigned when view controller's view is loading and presented to user.

So it is null until you display your view controller somehow.

In this code, you haven't added firstPage's view to anywhere yet, so it returns null.

- (void)viewDidLoad {
     [super viewDidLoad];
     FirstViewController *firstPage = [[FirstViewController alloc] init];
     _secondTField.text = firstPage.firstTField.text;
}

Your firstViewController's IBOutlets have not been initialized yet. Create a string property. Set that. Then in your firstViewController's viewDidLoad set the textField's text property.

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