简体   繁体   中英

Passing UITextField text to UILabel on different View Controller

I am trying to pass UITextField's text from View Controller 1 to the UILabel in View Controller 2.

I am using segue to pass the information, but I am not getting anything on Label. It seems like the text from the text field becomes NULL in when it is passed to view controller 2.

View Controller 1 (UITextField)

- (IBAction)sendtoVC2:(UIButton *)sender
{
    [self performSegueWithIdentifier:@"toVC2" sender:self];
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"toVC2"])
    {
        ViewController2 *VC2 = (ViewController2 *)segue.destinationViewController;
        VC2.label.text = self.textField.text;
    }
}

View Controller 2.h (UILabel)

#import "ViewController1.h"

@interface ViewController2 : UIViewController
@property (strong, nonatomic) IBOutlet UILabel *label;
@end

Thank you.

Most likely, it happens because the label doesn't exist yet. When UIViewController is created, its view is not loaded until it is actually required. This is called 'lazy loading', which means that a value is created only when somebody requires it for the first time.

Lazy loading is a design pattern commonly used in computer programming to defer initialization of an object until the point at which it is needed. It can contribute to efficiency in the program's operation if properly and appropriately used.

Your UILabel property in second view controller gets a value only after second view controller's view is loaded, which happens only when somebody explicitly calls its 'view' property. So, to prove my theory just insert one more line:

if ([segue.identifier isEqualToString:@"toVC2"])
{
    ViewController2 *VC2 = (ViewController2 *)segue.destinationViewController;
    UIView *unusedReferenceToViewToLoadTheView = VC2.view
    VC2.label.text = self.textField.text;
}

In viewDidLoad of View Controller 2, the text is lost, because the label was not created yet.

You have to save the text in a variable, and in viewDidLoad of ViewController2, set the text in label.

Like this:

View Controller 1:

 - (IBAction)sendtoVC2:(UIButton *)sender
{
    [self performSegueWithIdentifier:@"toVC2" sender:self];
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"toVC2"])
    {
        ViewController2 *VC2 = (ViewController2 *)segue.destinationViewController;
        VC2.text = self.textField.text;
    }
}

View Controller 2.h:

 #import "ViewController1.h"

@interface ViewController2 : UIViewController
@property (strong, nonatomic) IBOutlet UILabel *label;
@property (nonatomic, copy) NSString *text;
@end

View Controller 2.m:

- (void)viewDidLoad {
    [super viewDidLoad];
    label.text = text;
}

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