今天复习两个界面的传值。有两个界面,当点击第一个界面的按钮时,第一个界面的UITextField的值就会传给第二个界面的UITextField,同理,当点击第二个界面的按钮时,第二个界面的UITextField的值就会传给第一个界面的UITextField。
首先创建两个继承于ViewController的类,分别是MainViewController and SecondViewController.
MainViewController.h
//设置两个属性
@property (nonatomic,retain) UITextField *firstTextField;
@property (nonatomic,retain) NSString *firstString;
mainViewController.m
#import "SecondViewController.h"
//当第二个页面给第一个页面传值时需要用到协议
@interface MainViewController ()<SecondViewCoontrollerDelegate>
@end
@implementation MainViewController
-(void)dealloc
{
[_firstTextFieldrelease];
[_firstString release];
[superdealloc];
}
-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [superinitWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
}
return self;
}
- (void)viewDidLoad {
[superviewDidLoad];
[self.viewsetBackgroundColor:[UIColorwhiteColor]];
[selfcreate];
}
- (void)create
{
self.firstTextField = [[UITextFieldalloc] initWithFrame:CGRectMake(100,200, 175, 40)];
[_firstTextFieldsetBorderStyle:3];
[_firstTextFieldsetText:_firstString];
[self.viewaddSubview:_firstTextField];
[_firstTextFieldrelease];
UIButton *button = [UIButtonbuttonWithType:UIButtonTypeRoundedRect];
[buttonsetFrame:CGRectMake(200,300, 70, 40)];
[button setTitle:@"按钮"forState:UIControlStateNormal];
[button addTarget:selfaction:@selector(buttonAction:)forControlEvents:UIControlEventTouchUpInside];
[self.viewaddSubview:button];
}
- (void)buttonAction:(id)sender
{
SecondViewController *secondViewController = [[SecondViewControlleralloc] init];
[secondViewControllersetDelegate:self];
secondViewController.secondString =self.firstTextField.text;
[self.navigationControllerpushViewController:secondViewController animated:YES];
[secondViewControllerrelease];
}
- (void)sendMessageToFirstViewController:(NSString *)text
{
self.firstTextField.text = text;
}
@protocol SecondViewCoontrollerDelegate <NSObject>
- (void)sendMessageToFirstViewController:(NSString *)text;
@end
@interface SecondViewController :UIViewController
@property (nonatomic,retain) UITextField *secondTextField;
@property (nonatomic,retain) NSString *secondString;
//协议第二步:指定代理人
@property (nonatomic,assign) id<SecondViewCoontrollerDelegate>delegate;
-(void)dealloc
{
[_secondStringrelease];
[_secondTextFieldrelease];
[superdealloc];
}
- (void)viewDidLoad {
[superviewDidLoad];
[self.viewsetBackgroundColor:[UIColorwhiteColor]];
[selfcreate];
}
- (void)create
{
self.secondTextField = [[UITextFieldalloc] initWithFrame:CGRectMake(100,200, 175, 40)];
[_secondTextFieldsetBorderStyle:3];
[_secondTextFieldsetText:_secondString];
[self.viewaddSubview:_secondTextField];
[_secondTextFieldrelease];
UIButton *button = [UIButtonbuttonWithType:UIButtonTypeRoundedRect];
[buttonsetFrame:CGRectMake(200,300, 70, 40)];
[button setTitle:@"按钮"forState:UIControlStateNormal];
[button addTarget:selfaction:@selector(buttonAction:)forControlEvents:UIControlEventTouchUpInside];
[self.viewaddSubview:button];
}
- (void)buttonAction:(id)sender
{
[self.navigationController popToRootViewControllerAnimated:YES];
if ([self.delegaterespondsToSelector:@selector(sendMessageToFirstViewController:)]) {
[self.delegatesendMessageToFirstViewController:self.secondTextField.text];
}
}
AppDelegate.m
-(void)dealloc
{
[_secondString release];
[_secondTextField release];
[super dealloc];
}
- (void)viewDidLoad {
[super viewDidLoad];
[self.view setBackgroundColor:[UIColor whiteColor]];
[self create];
}
- (void)create
{
self.secondTextField = [[UITextField alloc] initWithFrame:CGRectMake(100, 200, 175, 40)];
[_secondTextField setBorderStyle:3];
[_secondTextField setText:_secondString];
[self.view addSubview:_secondTextField];
[_secondTextField release];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setFrame:CGRectMake(200, 300, 70, 40)];
[button setTitle:@"按钮" forState:UIControlStateNormal];
[button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
- (void)buttonAction:(id)sender
{
[self.navigationController popToRootViewControllerAnimated:YES];
//协议第三步:设置方法
if ([self.delegate respondsToSelector:@selector(sendMessageToFirstViewController:)]) {
[self.delegate sendMessageToFirstViewController:self.secondTextField.text];
}
}
Appdelegate.m
-(void)dealloc
{
[_window release];
[super dealloc];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[_window setBackgroundColor:[UIColor whiteColor]];
[_window makeKeyAndVisible];
[_window release];
MainViewController *mainViewController = [[MainViewController alloc] init];
UINavigationController *navigationController = [[UINavigationController alloc] init];
[_window setRootViewController: navigationController];
[navigationController pushViewController:mainViewController animated:YES];
[navigationController release];
[mainViewController release];
return YES;
}