如何在视图之间传递文本 [英] how to pass text between views

查看:107
本文介绍了如何在视图之间传递文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我进行了2次浏览 我想将主视图上的标签文本发送到子视图 一个想要在另一个标签的文本值上打印的地方... 如何传递文字

i made 2 views and i want to send text of label on main view to sub view to an want to print it there on another label's text value.... how to pass that text

推荐答案

我不会使用单例模式或任何其他全局变量".这将使您的视图控制器紧密耦合,并限制了可重用性.我只需要在第二个视图控制器中创建一个实例变量,然后在显示视图之前在主变量中进行设置即可.

I wouldn't use a singleton pattern or any other 'global variable'. This will make your view controllers very tightly coupled and restricts reusability. I would just create a instance variable in the second view controller and set this in the main one before presenting the view.

然后,第二个视图控制器将label.text设置为viewDidLoad中的实例变量.

The second view controller then sets the label.text to the instance variable in (for example) viewDidLoad.

这样,第二个视图控制器就不依赖于任何全局变量"或包含它,并且将更可重用.

This way, the second view controller doesn't depend on any 'globals' or includes and will be more reusable.

//SecondViewController.h
@interface SecondViewController : UIViewController {
    NSString *theLabel;
}

@property(nonatomic, copy) NSString *theLabel; //Synthesize in implementation

@end

然后在主视图控制器中:

Then in the main view controller:

//Create instance of secondViewController
instanceOfSecondViewController.theLabel = @"Nut";
//Present the second view here

这篇关于如何在视图之间传递文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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