Xcode Storyboard - Transfer of data

2019-09-08 16:40发布

问题:

I'm new to storyboarding so the answer to this may be simplistic,

I've created a viewController in which a UITextField is present. My test is to transfer the data (text) from that text field into a viewController that is pushed onto the screen.

The coding that i have is as follows:

ViewController1.h -

#import <UIKit/UIKit.h>
#import "ViewController2.h"

@interface ViewController1 : UIViewController

@property (nonatomic, retain) IBOutlet UITextField *inputText;

@end

ViewController1.m

#import "ViewController1.h"

@implementation ViewController
@synthesize inputText;


-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    if ([[segue identifier] isEqualToString:@"showTextController"]) {

    ViewController2 *vc2 = [segue destinationViewController];
    vc2.selectedText.text = self.inputText.text;


    }

}

@end

ViewController2.h -

#import <UIKit/UIKit.h>


@interface ViewController2 : UIViewController 

@property (nonatomic, retain) IBOutlet UILabel *selectedText;

@end

ViewController2.m

#import "ViewController2.h"

@implementation ViewController2
@synthesize selectedText;

@end

The segue between viewController1 and 2 in storyboard is referred to as 'showTextController'.

Is this the correct coding for something so simple? Do i need to be using 'ViewDidLoad' method along with the prepareForSegue:sender method?

回答1:

It looks correct to me. In fact, this is simpler than we have had in the past since the storyboard takes care instantiating our view controller objects. One thing to note is that if you're using ARC then you shouldn't be retaining your UILabel.

Hope this helps.