举个例子,第一个page(即UIViewController)显示天气,需要对所在地进行设置,这就需要跳转到第二个page,选择好所在地之后,将所在地信息(即返回参数)传回第一个page。
第一种:通过Delegate的Protocol
1.新建PassValueDelegate.h
#import <Foundation/Foundation.h> @protocol PassValueDelegate <NSObject> -(void)passValue:(NSString *)value; @end
2.在需要得到返回值的UIViewController.h添加对PassValueDelegate的实现
@interface IkrboyViewController6 : UIViewController<PassValueDelegate>
3.在UIViewController.m实现-(void)passValue的方法,即处理得到的返回值的事件
-(void)passValue:(NSString *)value{ NSLog(@"get backcall value=%@",value); }
4.在下一个UIViewController.h(即为上一个UIViewController提供返回数据)添加Delegate的参数
@property(nonatomic,assign) NSObject<PassValueDelegate> *delegate;
5.在上一个UIViewController跳转到下一个UIViewController之前添加代码
//设置第二个窗口中的delegate为第一个窗口的self newViewController.delegate = self;
6.下一个UIViewController返回到上一个UIViewController的代码
self dismissViewControllerAnimated:YES completion:^{ //通过委托协议传值 [self.delegate passValue:@"ululong"]; }];
第二种:绑定Notification,利用userInfo参数
1.在第一个UIViewController的viewDidLoad添加注册RegisterCompletionNotification代码
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(registerCompletion:) name:@"RegisterCompletionNotification" object:nil];
2.别忘了解除·Notification
- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. [[NSNotificationCenter defaultCenter] removeObserver:self]; }
3.实现registCompletion方法
-(void)registerCompletion:(NSNotification*)notification { //接受notification的userInfo,可以把参数存进此变量 NSDictionary *theData = [notification userInfo]; NSString *username = [theData objectForKey:@"username"]; NSLog(@"username = %@",username); }
4.在下一个UIViewController的返回操作中添加代码
NSDictionary *dataDict = [NSDictionary dictionaryWithObject:@"MissA" forKey:@"username"]; [[NSNotificationCenter defaultCenter] postNotificationName:@"RegisterCompletionNotification" object:nil userInfo:dataDict]; [self dismissViewControllerAnimated:YES completion:nil];
相关推荐
iOS5 UIViewController 新特性 UIViewController的切换
iOS 多个UIViewController之间滑动切换
IOS学习之UiViewController带值跳转以及协议的实现,原来这么做的啊 http://blog.csdn.net/xiaoyuan511
自己写了一个Dome 实现了UIView或者时UIViewController 缩放动画效果
IOS弹出自定义UIViewControllerIOS弹出自定义UIViewController
AICustomViewControllerTransition, 为iOS创建定制UIViewController转换的简单而简洁的方法 AICustomViewControllerTransition为iOS创建定制UIViewController转换的简单而简洁的方法没有限制,也没有实现...
系统返回按钮事件拦截 Swift 版本
UIViewController是IOS程序中的一个重要组成部分,下面这篇文章主要给大家介绍了关于iOS开发教程之UIView和UIViewController的生命周期的相关资料,文中通过示例代码介绍的非常详细,需要的朋友可以参考下
iOS, UIViewController, UIView, UIViewController的加载方式和UIView的加载详解
UIViewController+Present,这个分类是用来适配iOS13之后UIViewController模态弹出问题的. 使用这种方式就不用去项目中挨个找模态弹出了.
UIViewController添加多个UIViewController。可以把UIViewController的View当作容器,让其他UIViewController添加进该View,并可以在多个UIViewController间切换。
使用runtime机制,隐藏iOS导航栏返回按钮标题的文字,并不会有其他的隐患。
主要介绍了iOS开发中的UIViewController,其中以UIViewController作为着重讲解,需要的朋友可以参考下
作者HeathWang,源码HWPopController,通过UIViewController来编写弹窗,支持所有的UIViewController,支持转屏,提供默认动画效果,并可以高度自定义。
《用户协议及隐私政策》 弹框的实现步骤: 1、自定义TextView,采用富文本属性进行内容设置attributedText(包括下划线NSUnderlineStyleSingle、超链接NSLinkAttributeName 、颜色NSForegroundColorAttributeName 等...
iOS中的UIViewController是UIKit框架中最基本的一个类。从第一个UI视图到复杂完整项目,都离不开UIViewController作为基础。基于UIViewController的封装和扩展,也能够出色的完成各种复杂界面逻辑。这里旨在讨论...
BaseViewController是基于UIViewController下的封装,添加了一些基本常用的类型.比如创建Button,创建label,数据解析工具等等.思想非常好,工作后,项目上经常用到. 有很重要的参考价值
作者heroims,源码IOS11SytleTitleController,IOS11 大标题风格UIViewController, 建议利用UIViewControllerScroll 自己写更多更炫的效果。
UIViewController Demo源代码 创建带nib和不带nib文件
UIViewController生命周期详解及方法调用