实现从secondViewController中的视图向viewController中的视图传值
首先:创建协议PassValueDelegate.h
//
// AppDelegate.h
// PassValueByDelegate
//
// Created by 唐韧 on 12-8-28.
// Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//
#import <UIKit/UIKit.h>
@class ViewController;
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) ViewController *viewController;
@end
在secondViewController创建代理
#import <UIKit/UIKit.h>
#import "PassValueDelegate.h"
@interface SecondViewController : UIViewController
@property (retain, nonatomic) IBOutlet UITextField *nameTextField;
@property (retain, nonatomic) IBOutlet UITextField *ageTextFiled;
@property (retain, nonatomic) IBOutlet UITextField *gendarTextField;
//这里用assign而不用retain是为了防止引起循环引用。
@property(nonatomic,assign) NSObject<PassValueDelegate> *delegate;
- (IBAction)okBtnClicked:(id)sender;
- (IBAction)closeKeyboard:(id)sender;
@end
@implementation SecondViewController
@synthesize nameTextField;
@synthesize ageTextFiled;
@synthesize gendarTextField;
@synthesize delegate;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
self.navigationItem.title = @"Second";
}
- (void)viewDidUnload
{
[self setNameTextField:nil];
[self setAgeTextFiled:nil];
[self setGendarTextField:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (void)dealloc {
[nameTextField release];
[ageTextFiled release];
[gendarTextField release];
[super dealloc];
}
- (IBAction)okBtnClicked:(id)sender {
UserEntity *userEntity = [[UserEntity alloc] init];
userEntity.userName = self.nameTextField.text;
userEntity.gendar = self.gendarTextField.text;
userEntity.age = [self.ageTextFiled.text intValue];
//通过委托协议传值
[self.delegate passValue:userEntity];
//退回到第一个窗口
[self.navigationController popViewControllerAnimated:YES];
[userEntity release];
}
/*单击屏幕其他区域关闭键盘的方法
实现方法是:首先选中xib文件的view,设置class为UIControl
然后在事件中选择Touch Down拖线到.h文件中声明该方法,最后实现下面即可
*/
- (IBAction)closeKeyboard:(id)sender {
[self.nameTextField resignFirstResponder];
[self.ageTextFiled resignFirstResponder];
[self.gendarTextField resignFirstResponder];
}
@end
在viewController实现代理,实现值传递
#import <UIKit/UIKit.h>
#import "PassValueDelegate.h"
//第一个窗口遵守PassValueDelegate
@interface ViewController : UIViewController<PassValueDelegate>
@property (retain, nonatomic) IBOutlet UILabel *nameLabel;
@property (retain, nonatomic) IBOutlet UILabel *ageLabel;
@property (retain, nonatomic) IBOutlet UILabel *gendarLabel;
- (IBAction)openBtnClicked:(id)sender;
@end
@implementation ViewController
@synthesize nameLabel;
@synthesize ageLabel;
@synthesize gendarLabel;
//实现协议,在第一个窗口显示在第二个窗口输入的值,类似Android中的onActivityResult方法
-(void)passValue:(UserEntity *)value
{
self.nameLabel.text = value.userName;
self.ageLabel.text = [NSString stringWithFormat:@"%d",value.age];
self.gendarLabel.text = value.gendar;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.navigationItem.title = @"First";
}
- (void)viewDidUnload
{
[self setNameLabel:nil];
[self setAgeLabel:nil];
[self setGendarLabel:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
- (void)dealloc {
[nameLabel release];
[ageLabel release];
[gendarLabel release];
[super dealloc];
}
//点击进入第二个窗口的方法
- (IBAction)openBtnClicked:(id)sender {
SecondViewController *secondView = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:[NSBundle mainBundle]];
//设置第二个窗口中的delegate为第一个窗口的self
secondView.delegate = self;
[self.navigationController pushViewController:secondView animated:YES];
[secondView release];
}
@end