原创Blog,转载请注明出处
http://blog.csdn.net/hello_hwc?viewmode=contents
本Demo的内容很简单,两个View,各含有textfield,第一个ViewController使用Modal Segue切换到第二个Segue时候,把Textfied的内容传到第二个ViewController的Textview,
第二个ViewController点击Done会传递数据给第一个ViewController,点击Cancel则直接返回。
附上完整的工程代码链接
http://pan.baidu.com/s/1mg3eOHy
最后的界面如图
一 新建一个工程,使用语言选择Objective C,完成(删除默认的ViewController和清空Stroyboard)。
二 在Stroyboard上依照下图拖出两个UIViewController
三 新建两个ViewController Class,命名为firstViewController,SecondViewController
分别将两个ViewController的类设置为firstViewController,SecondViewController
四 建立Modal Segue 和Unwind Segue
在Stroyboard上Control + 鼠标拖拽Pass按钮,到下一个ViewController,选择Present Modally,这样就建立一个Modal Segue
在Stroyboard上,将FirstViewController的Textfied创建Outlet到.m文件,将SecondViewController的Textfield创建Outlet到.h文件
在FirstViewController中键入如下代码
注意:一定要是返回值是IBAction类型,参数是UIStoryboardSegue*
-(IBAction)done:(UIStoryboardSegue *)segue{ if ([segue.sourceViewController isKindOfClass:[SecondViewController class]]) { SecondViewController * svc = (SecondViewController *)segue.sourceViewController; self.textField.text = svc.textField.text; } }
这段代码涌来处理Unwind也就是返回的时候的数据。
然后,这次当然也可以在Stroyboard上拖拽,不过这次我们选择在大纲里拖拽,Control+拖拽Done到Exit上,会弹出我们刚才设置的done函数,选择改函数就创建成功了UnwindSegue了
然后,附上完整的代码及注释‘
firstViewController.h
</pre></p><pre name="code" class="objc">// // firstViewController.m // ModalSegueForCSDN // // Created by huangwenchen on 14/12/26. // Copyright (c) 2014年 huangwenchen. All rights reserved. // #import "firstViewController.h" #import "SecondViewController.h" @interface firstViewController()<UITextFieldDelegate> @property (weak, nonatomic) IBOutlet UITextField *textField; @end @implementation firstViewController - (IBAction)pass:(id)sender { } #pragma mark - prepare for segue //firstViewController传数据到SecondViewController -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { if ([segue.destinationViewController isKindOfClass:[SecondViewController class]]) { SecondViewController * dvc = (SecondViewController*)segue.destinationViewController; dvc.text = self.textField.text; } } #pragma mark - unwind segue //SecondViewController传到firstViewController -(IBAction)done:(UIStoryboardSegue *)segue{ if ([segue.sourceViewController isKindOfClass:[SecondViewController class]]) { SecondViewController * svc = (SecondViewController *)segue.sourceViewController; self.textField.text = svc.textField.text; } } //点击键盘上Return则键盘消失 -(BOOL)textFieldShouldReturn:(UITextField *)textField { [textField resignFirstResponder]; return YES; } @end
SecondViewController.h
// // SecondViewController.h // ModalSegueForCSDN // // Created by huangwenchen on 14/12/26. // Copyright (c) 2014年 huangwenchen. All rights reserved. // #import <UIKit/UIKit.h> @interface SecondViewController : UIViewController @property (strong,nonatomic)NSString * text;//接收数据的接口 @property (weak, nonatomic) IBOutlet UITextField *textField;//传出数据的接口 @end
SecondViewController.m
// // SecondViewController.m // ModalSegueForCSDN // // Created by huangwenchen on 14/12/26. // Copyright (c) 2014年 huangwenchen. All rights reserved. // #import "SecondViewController.h" @interface SecondViewController()<UITextFieldDelegate> @end @implementation SecondViewController //点击取消,直接退出 - (IBAction)cancel:(id)sender{ [self.presentingViewController dismissViewControllerAnimated:YES completion:NULL]; } //显示内容 -(void)viewWillAppear:(BOOL)animated { self.textField.text = self.text; } //点击键盘上Return则键盘消失 -(BOOL)textFieldShouldReturn:(UITextField *)textField { [textField resignFirstResponder]; return YES; } @end
FAQ
1 IBAction是什么类型
运行就是Void,只不过写成这个方便XCode进行识别