这次要讲的是如何从A进入B,在B输入值后回传给A,那么在IOS中实现这个功能就需要使用到Delegate(委托协议)。
其中有两个ViewController分别对应两个界面,一个协议PassValueDelegate用来实现传值协议,UserEntity是传递数据的对象。
协议中声明的方法:
#import <Foundation/Foundation.h> @class UserEntity; @protocol PassValueDelegate <NSObject> -(void)passValue:(UserEntity *)value; @end
在第一个窗口实现协议:
//第一个窗口遵守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
.m文件中实现协议的方法
//实现协议,在第一个窗口显示在第二个窗口输入的值 -(void)passValue:(UserEntity *)value { self.nameLabel.text = value.userName; self.ageLabel.text = [NSString stringWithFormat:@"%d",value.age]; self.gendarLabel.text = value.gendar; }
点击按钮所触发的事件:
//点击进入第二个窗口的方法 - (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]; }
第二个窗口中声明一个NSObject对象,该对象遵守PassValueDelegate协议:
@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
输入完毕后,点击按钮所触发的事件:
- (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]; }
相关推荐
在IOS中使用委托协议在两个View之间传值,类似Android中Activity的onActivirtyResult方法
IOS实现在两个ViewController间传值
ios实现viewcontroller切换传值的三种方法
IOS实现在两个ViewController间传值
ByValueTwoViewController2个ViewController之间传值的方式事例。delegate传值Block传值KVO传值消息传值
IOS 传值 变量 利用临时变量在两个ViewController之间传值
实现两个viewcontroller之间的界面切换,有动画效果
-- 使用Delegate在两个ViewController间传值 -- 内存泄露Demo leaky app -- iOS上拉加载更多的实现 -- iOS视频通话/直播demo -- 数据库操作(使用FMDB) -- iOS图文混排(模仿新浪微博) -- IOS高仿微信聊天对话界面 -...
现有两个视图(ViewController,ViewController1),从ViewController中带参数跳转到ViewController1,在ViewController1选中数据后带有效数据后退到ViewController中,委托实现上述功能。 可以参考:...
导航控制器presentViewController,导航控制器presentViewController的实现。
0代码实现viewcontroller 使viewcontroller更干净
ViewController.m
通脱截图做背景的方式实现玻璃效果模糊显示一个ViewController,
作者mengxianliang,源码XLBubbleTransition,iOS ViewController间切换的转场动画。
IOS父ViewController为竖屏,presentViewController强制旋转为横屏
集中管理 VC 的 方法类 集中管理 VC 的 方法类集中管理 VC 的 方法类集中管理 VC 的 方法类集中管理 VC 的 方法类集中管理 VC 的 方法类集中管理 VC 的 方法类集中管理 VC 的 方法类
RTRootNavigationController:让每一个 ViewController 都有自己独立的 NavigationBar.zip,隐式地使每个视图控制器都有自己的导航栏
iOS之presentViewController模态全屏适配解决方案.docx
Swift自定义presentViewController的转场动画
实现类似于Facebook的滑动和滚动拉消失的Viewcontroller