Xcode Storyboard - 数据传输 [英] Xcode Storyboard - Transfer of data

查看:29
本文介绍了Xcode Storyboard - 数据传输的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是故事板的新手,所以这个问题的答案可能很简单,

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

我创建了一个视图控制器,其中存在一个 UITextField.我的测试是将该文本字段中的数据(文本)传输到推送到屏幕上的 viewController 中.

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.

我的编码如下:

ViewController1.h -

ViewController1.h -

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

@interface ViewController1 : UIViewController

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

@end

ViewController1.m

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 -

ViewController2.h -

#import <UIKit/UIKit.h>


@interface ViewController2 : UIViewController 

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

@end

ViewController2.m

ViewController2.m

#import "ViewController2.h"

@implementation ViewController2
@synthesize selectedText;

@end

故事板中 viewController1 和 2 之间的转场称为showTextController".

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

对于这么简单的事情,这是正确的编码吗?我是否需要同时使用ViewDidLoad"方法和 prepareForSegue:sender 方法?

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

推荐答案

在我看来是正确的.事实上,这比我们过去更简单,因为故事板负责实例化我们的视图控制器对象.需要注意的一件事是,如果您使用的是 ARC,那么您不应该保留您的 UILabel.

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.

希望这会有所帮助.

这篇关于Xcode Storyboard - 数据传输的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆