@property (nonatomic, copy) NSString *textString;//属性传值
在m文件中的- (void)ViewDidLoad方法中定义一个UILabel
- (void)viewDidLoad {
[super viewDidLoad];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 100, CGRectGetWidth(self.view.bounds) - 40, 100)];
label.backgroundColor = [UIColor whiteColor];
label.font = [UIFont systemFontOfSize:20];
label.numberOfLines = 0;//表示无限行
label.text = self.textString;//使用属性传递得到的文本
[self.view addSubview:label];
[label release];
self.view.backgroundColor = [UIColor orangeColor];
}
2.在第一个根视图控制器中引入第二个根视图控制器
#import "RootViewController.h" 第一个根视图控制器
#import "DetailViewController.h" 第二个根视图控制器
在第一个根视图控制器中也就是#import "RootViewController.h"定义一个UITextFIeld属性
并用懒加载实现以下:
- (UITextField *)textField
{
if(!_textField)
{
self.textField = [[UITextField alloc] initWithFrame:CGRectMake(20, 100, CGRectGetWidth(self.view.bounds) - 40, 40)];
_textField.borderStyle = UITextBorderStyleRoundedRect;
}
return _textField;
}
- (void)viewDidLoad中设置一个button
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor greenColor];
[self.view addSubview:self.textField];
UIButton *pushButton = [UIButton buttonWithType:UIButtonTypeCustom];
pushButton.frame = CGRectMake(0, 0, 80, 40);
[pushButton setTitle:@"入栈显示" forState:UIControlStateNormal];
pushButton.titleLabel.font = [UIFont systemFontOfSize:20];
pushButton.center = self.view.center;
[pushButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
pushButton.showsTouchWhenHighlighted = YES;
[pushButton addTarget:self action:@selector(handlePushButton:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:pushButton];
[pushButton release];
}
- (void)handlePushButton:(UIButton *)sender
{
/// *********************////
DetailViewController *detailVC = [[DetailViewController alloc] init];
//创建完对象就位对应的属性textString赋值
detailVC.textString = self.textField.text;
[self.navigationController pushViewController:detailVC animated:YES];
}