程序经常会用到两个场景切换,并需要彼此传递数据,本文介绍ViewController和SecondViewController之间传递参数方式,界面效果如上图。
点击ViewController中的"Sent Second"按钮,会将文本编辑框中的内容传给SecondViewController中的文本编辑框中,点击SecondViewController中的"Return First"按钮,将其中文本编辑框中的内容传回ViewController。
1.ViewController -> SecondViewController:
首先在SecondViewController中定义一个变量:NSString *nsText;
.h文件:
@property (copy,nonatomic) NSString *nsText;
.m文件:@synthesize nsText;
ViewControlle中重写方法 prepareForSegue:,详细如下:
.h文件:
#import"XYSecondViewController.h"
.m文件
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
XYSecondViewController *xySecondViewController = segue.destinationViewController;
xySecondViewController.nsText =textField.text;
}
2.SecondViewController -> ViewController:
采用委托方法,首先在SecondViewController定义委托:
.h文件:
@protocol XYSecondViewControllerDelegate <NSObject>
- (void) addItem:(NSString*) text;
@end
@property (weak,nonatomic) id <XYSecondViewControllerDelegate> delegate;
.m文件:
@synthesize delegate;
在按钮响应方法中添加:
[self.delegateaddItem:textField.text];
ViewController中声明委托对象并实现委托XYSecondViewControllerDelegate 中的addItem:方法:
.h文件:
@interface ViewController :UIViewController <XYSecondViewControllerDelegate>
.m文件:
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// 声明委托对象,这个很重要
XYSecondViewController *xySecondViewController = segue.destinationViewController;
xySecondViewController.delegate =self;
// ...
}
- (void)addItem:(NSString *)text
{
[textFieldsetText:text];
}
完整代码:
//
// ViewController.h
// TestDelegate
//
// Created by皮特熊 on 15-5-15.
// Copyright (c) 2015年 __MyCompanyName__. All rights reserved.
//
#import<UIKit/UIKit.h>
#import"XYSecondViewController.h"
@interface ViewController :UIViewController <XYSecondViewControllerDelegate>
@property (weak,nonatomic) IBOutletUITextField *textField;
- (IBAction)doTextFieldEditEndBtn:(id)sender;
@end
//
// ViewController.m
// TestDelegate
//
// Created by皮特熊 on 15-5-15.
// Copyright (c) 2015年 __MyCompanyName__. All rights reserved.
//
#import"ViewController.h"
@interfaceViewController ()
@end
@implementation ViewController
@synthesize textField;
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
XYSecondViewController *xySecondViewController = segue.destinationViewController;
xySecondViewController.delegate =self;
xySecondViewController.nsText =textField.text;
}
- (void)viewDidLoad
{
[superviewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)viewDidUnload
{
[selfsetTextField:nil];
[superviewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation !=UIInterfaceOrientationPortraitUpsideDown);
}
- (void)addItem:(NSString *)text
{
[textFieldsetText:text];
}
- (IBAction)doTextFieldEditEndBtn:(id)sender {
[senderresignFirstResponder];
}
@end
//
// XYSecondViewController.h
// TestDelegate
//
// Created by皮特熊 on 15-5-15.
// Copyright (c) 2015年 __MyCompanyName__. All rights reserved.
//
#import<UIKit/UIKit.h>
@protocol XYSecondViewControllerDelegate <NSObject>
- (void) addItem:(NSString*) text;
@end
@interface XYSecondViewController :UIViewController
- (IBAction)textFieldDoneEdit:(id)sender;
@property (weak,nonatomic) IBOutletUITextField *textField;
- (IBAction)doBackFirstBtn:(id)sender;
@property (weak,nonatomic) id <XYSecondViewControllerDelegate> delegate;
@property (copy,nonatomic) NSString *nsText;
@end
//
// XYSecondViewController.m
// TestDelegate
//
// Created by 皮特熊 on 15-5-15.
// Copyright (c) 2015年 __MyCompanyName__. All rights reserved.
//
#import"XYSecondViewController.h"
@interfaceXYSecondViewController ()
@end
@implementation XYSecondViewController
@synthesize textField;
@synthesize delegate;
@synthesize nsText;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[superviewDidLoad];
// Do any additional setup after loading the view.
if (nsText != nil) {
textField.text =nsText;
}
}
- (void)viewDidUnload
{
delegate = nil;
nsText = nil;
[selfsetTextField:nil];
[superviewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (IBAction)doBackFirstBtn:(id)sender {
[self.delegateaddItem:textField.text];
[selfdismissModalViewControllerAnimated:YES];
}
- (IBAction)textFieldDoneEdit:(id)sender {
[senderresignFirstResponder];
}
@end