![创建文件](https://img-blog.csdn.net/20150929085903209)
RootViewController.m
#import "RootViewController.h"
#import "SecondViewController.h"
@interface RootViewController ()
@property(nonatomic, retain)UITextField *myTextField;
@property(nonatomic, retain)UILabel *myLabel;
@end
@implementation RootViewController
- (void)dealloc
{
[_myTextField release];
[_myLabel release];
[super dealloc];
}
- (void)viewDidLoad {
[super viewDidLoad];
1.创建一个textField.
self.myTextField = [[UITextField alloc] initWithFrame:CGRectMake(100, 100, 150, 50)];
self.myTextField.layer.borderWidth = 1;
self.myTextField.layer.cornerRadius = 10;
[self.view addSubview:self.myTextField];
[_myTextField release];
2.创建label.
self.myLabel = [[UILabel alloc] initWithFrame:CGRectMake(100, 200, 150, 50)];
self.myLabel.layer.borderWidth = 1;
self.myLabel.layer.cornerRadius = 10;
[self.view addSubview:self.myLabel];
[_myLabel release];
3.创建一个下一页的button.
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
button.frame = CGRectMake(100, 300, 150, 50);
[button setTitle:@"下一页" forState:UIControlStateNormal];
button.layer.borderWidth = 1;
button.layer.cornerRadius = 10;
[self.view addSubview:button];
[button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
}
- (void)buttonAction:(UIButton *)button {
4.点击下一页,打印textField里的内容.
NSLog(@"%@", self.myTextField.text);
5.push下一页.
SecondViewController *secondVC = [[SecondViewController alloc] init];
[self.navigationController pushViewController:secondVC animated:YES];
[secondVC release];
7.利用属性传值.
secondVC.str = self.myTextField.text;
}
SecondViewController.h
#import <UIKit/UIKit.h>
@interface SecondViewController : UIViewController
6.先写一条属性.
@property(nonatomic, copy)NSString *str;
@end
SecondViewController.m
#import "SecondViewController.h"
#import "RootViewController.h"
@interface SecondViewController ()
@property(nonatomic, retain)UITextField *textField;
@end
@implementation SecondViewController
- (void)dealloc
{
[_textField release];
[super dealloc];
}
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
NSLog(@"%@", self.str);
self.textField = [[UITextField alloc] initWithFrame:CGRectMake(100, 100, 150, 50)];
self.textField.layer.borderWidth = 1;
self.textField.layer.cornerRadius = 10;
[self.view addSubview:self.textField];
[_textField release];
7.在后一个界面中用textField.text来接收这个属性
self.textField.text = self.str;
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
button.frame = CGRectMake(100, 200, 150, 50);
[button setTitle:@"返回" forState:UIControlStateNormal];
button.layer.borderWidth = 1;
button.layer.cornerRadius = 10;
[self.view addSubview:button];
[button addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
}
- (void)backAction:(UIButton *)button {
8.返回上一页.
[self.navigationController popViewControllerAnimated:YES];
}