Xcode Storyboard - Transfer of data

删除回忆录丶 提交于 2020-01-06 09:09:25

问题


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.



来源:https://stackoverflow.com/questions/7920229/xcode-storyboard-transfer-of-data

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!