通知传值
通知是在跳转控制器之间常用的传值代理方式。NSNotificationCenter提供了一种解耦的方式,就是任何对象都可以发送通知到中心,同时任何对象可以监听中心的通知。

  • 发送通知(传值页面)
//通知中心NSNotificationCenter,发送通知- (IBAction)changeColorAction2:(id)sender {UIColor *color = [UIColor greenColor];[[NSNotificationCenter defaultCenter] postNotificationName:@"ChangeColorKey" object:color];[[self navigationController] popViewControllerAnimated:YES];
}
  • 注册通知(取值页面)
//注册通知[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeColor:) name:@"ChangeColorKey" object:nil];
  • 响应接收通知
//响应通知中心NSNotificationCenter- (void)changeColor:(NSNotification *)notification{if([notification object] != nil){UIColor *color = [notification object];[self.view setBackgroundColor:color];}
}
  • 移除通知
- (void)dealloc {//移除通知[[NSNotificationCenter defaultCenter] removeObserver:self name:@"ChangeColorKey" object:nil];
}

注意:注意参数notification Observer为要删除的观察者,一定不能置为nil。

协议传值
A页面push到B页面,如果B页面的信息想回传(回调)到A页面,就用代理传值,其中B定义协议和声明代理,A确认并实现代理,A作为B的代理。

  • 在需要传值给其他类的类头文件中定义一个协议
@protocol ViewControllerAdelegate-(void)changeBgColorFromCtrlA:(ViewControllerA *)aView withColor:(UIColor *)color;@end
  • 在该类中声明一个代理属性
@interface ViewControllerA : UIViewController@property( assign, nonatomic ) id< ViewControllerAdelegate > delegate;@end
  • 在.m中实现
@implementation ViewControllerA
@synthesize delegate;
  • 在需要触发传值的方法中调用协议中的方法
//使用协议代理Delegate
-(IBAction)changeColorAction:(id)sender{[delegate changeBgColorFromCtrlA:self withColor:[UIColor grayColor]];[[self navigationController] popViewControllerAnimated:YES];
}
  • 在传值给的类中的.h文件中引用该协议
@interface ViewControllerB : UIViewController<ViewControllerAdelegate>@end
  • 在.m中
ViewControllerA *_ViewControllerA = [stryBoard instantiateViewControllerWithIdentifier:@"ViewControllerA"];_ViewControllerA.delegate = self;
  • 然后实现该方法
//响应协议代理Delegate
-(void)changeBgColorFromCtrlA:(ViewControllerA *)aView withColor:(UIColor *)color{[self.view setBackgroundColor:color];
}

顺便附上个人写的”通知传值“和”协议传值“的全部demo

  • ViewControllerA的.h文件
//  Created by Ydw on 16-03-17.
//  Copyright (c) 2016年 com. All rights reserved.
//#import <UIKit/UIKit.h>
@class ViewControllerA;@protocol ViewControllerAdelegate-(void)changeBgColorFromCtrlA:(ViewControllerA *)aView withColor:(UIColor *)color;@end@interface ViewControllerA : UIViewController@property( assign, nonatomic ) id< ViewControllerAdelegate > delegate;-(IBAction)changeColorAction:(id)sender;
-(IBAction)changeColorAction2:(id)sender;@end
  • ViewControllerA的.m文件
//  Created by Ydw on 16-03-17.
//  Copyright (c) 2016年 com. All rights reserved.
//#import "ViewControllerA.h"
#import "ViewControllerB.h"
@interface ViewControllerA ()@end@implementation ViewControllerA
@synthesize delegate;- (void)viewDidLoad
{[super viewDidLoad];self.title = @"Ctrl A";
}//使用协议代理Delegate
- (IBAction)changeColorAction:(id)sender {[delegate changeBgColorFromCtrlA:self withColor:[UIColor grayColor]];[[self navigationController] popViewControllerAnimated:YES];
}//通知中心NSNotificationCenter,发送通知
- (IBAction)changeColorAction2:(id)sender {UIColor *color = [UIColor greenColor];[[NSNotificationCenter defaultCenter] postNotificationName:@"ChangeColorKey" object:color];[[self navigationController] popViewControllerAnimated:YES];
}- (void)didReceiveMemoryWarning
{[super didReceiveMemoryWarning];// Dispose of any resources that can be recreated.
}@end
  • ViewControllerB的.h文件
//  Created by Ydw on 16-03-17.
//  Copyright (c) 2016年 com. All rights reserved.
//#import <UIKit/UIKit.h>
#import "ViewControllerA.h"
@interface ViewControllerB : UIViewController<ViewControllerAdelegate>@end
  • ViewControllerB的.m文件
//  Created by Ydw on 16-03-17.
//  Copyright (c) 2016年 com. All rights reserved.
//#import "ViewControllerB.h"@implementation ViewControllerB- (void)viewDidLoad
{[super viewDidLoad];self.title = @"Ctrl B";self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Go CtrlA" style:UIBarButtonItemStylePlain target:self action:@selector(go)];//注册通知[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeColor:) name:@"ChangeColorKey" object:nil];
}-(void)go {UIStoryboard *stryBoard=[UIStoryboard storyboardWithName:@"Main" bundle:nil];ViewControllerA *_ViewControllerA = [stryBoard instantiateViewControllerWithIdentifier:@"ViewControllerA"];_ViewControllerA.delegate = self;[[self navigationController] pushViewController:_ViewControllerA animated:YES];_ViewControllerA = nil;}//响应协议代理Delegate
-(void)changeBgColorFromCtrlA:(ViewControllerA *)aView withColor:(UIColor *)color{[self.view setBackgroundColor:color];
}//响应通知中心NSNotificationCenter
- (void)changeColor:(NSNotification *)notification{if([notification object] != nil){UIColor *color = [notification object];[self.view setBackgroundColor:color];}
}- (void)dealloc{//移除通知[[NSNotificationCenter defaultCenter] removeObserver:self name:@"ChangeColorKey" object:nil];
}- (void)didReceiveMemoryWarning
{[super didReceiveMemoryWarning];// Dispose of any resources that can be recreated.
}@end

属性传值
属性传值是将A页面所拥有的信息通过属性传递到B页面使用。B页面定义了一个属性,在A页面中直接通过属性赋值将A页面中的值传到B页面。(相对而言,最简单一种的传值,就不具体说明了)

  • 首先在SecondViewController视图中(即B页面)需要有一个属性用来存储传递过来的值:
@property(nonatomic,retain) NSString *firstValue;//属性传值
  • 然后MainViewController视图(即A页面)需要引用SecondViewController视图的头文件,在视图中的按钮点击事件中,通过SecondViewController的对象将需要传递的值存在firstValue中:
 - (void)buttonAction:(UIButton *)button {SecondViewController *second = [[SecondViewController alloc]init];second.firstValue = _txtFiled.text;[self.navigationController pushViewController:second animated:YES];
}
  • 页面跳转之后,就能在SecondViewController视图中,通过存值的属性,取用刚才传递过来的值:
[_txtFiled setText:_firstValue];//显示传过来的值,firstValue保存传过来的值

NSUserDefaults
1、NSUserDefaults记录本地一些轻量级的数据,是单例类。其通过userDefaults对象来将数据保存到NSUserDefaults的plist文件中,这个文件实际上是一个plist文件,在沙盒中的/Library/Preferences
/NSUserDefaults.plist 这个位置中。
2、NSUserDefaults支持的数据格式有:NSNumber(Integer、Float、Double),NSString,NSDate,NSArray,NSDictionary,BOOL类型。
3、NSUserDefaults是本地数据持久化的一种,可以投机取巧地进行数据传值,通过给NSUserDefaults设置一个DefaultsKey,就可以在任何页面通过DefaultsKey来取到值。简单的举例说明一下:

  • NSUserDefaults存值
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];NSMutableArray *rootArray = nil;if ([defaults objectForKey:DefaultsKey]) {rootArray = [NSMutableArray arrayWithArray:[defaults objectForKey:DefaultsKey]];}else {rootArray = [NSMutableArray array];}NSDictionary *infoDictionary = @{NameKey:@"Ydw",PhoneKey: @"1314520"};[rootArray addObject:infoDict];[defaults setObject:rootArray forKey:DefaultsKey];[defaults synchronize];
  • NSUserDefaults取值
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];NSArray *rootArray = [defaults objectForKey:DefaultsKey];if (rootArray && rootArray.count > 0) {NSLog(@"%@", rootArray);}else {NSLog(@"读取数据失败!");}
  • NSUserDefaults删除数据
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];[defaults removeObjectForKey:DefaultsKey];[defaults synchronize];NSLog(@"数据删除成功!");

KVC
KVC是Key Value Coding的缩写,即是键值编码。在iOS中,提供了一种方法通过使用属性的名称(也就是Key)来间接访问对象的属性方法。实际上,就是通过类定义我们可以看到类的各种属性,那么使用属性的名称就能访问到类实例化后的对象的这个属性值。这个方法可以不通过getter/setter方法来访问对象的属性。
NSArray/NSSet等都支持KVC。

  • 定义一个myInformation的类
@interface myInformation : NSObject {  NSString *_name;  int      _age;  int      _height;  int      _weight;
}
@end  
  • 声明属性对象
@interface myViewController : UIViewController  @property (nonatomic, retain) myInformation *information;  @end  
  • 使用KVC读取和改变类中的属性的值
 - (void)useKVC {  information = [[myInformation alloc] init];    NSLog(@"information's init height = %@", [information valueForKey:@"height"]);  [information setValue:[NSNumber numberWithInt:168] forKey:@"height"];  NSLog(@"information's height = %@", [information valueForKey:@"height"]);
} 
  • KVC的常用方法
- (id)valueForKey:(NSString *)key;
- (void)setValue:(id)value forKey:(NSString *)key;

iOS之界面传值(通知,属性,协议,NSUserDefaults,KVC)相关推荐

  1. 页面传值:属性,协议,Block传值

    1.属性传值和协议传值 1 #import "RootViewController.h" 2 #import "SecondViewController.h" ...

  2. ios 给网页传值_iOS学习——页面的传值方式

    2.4 通知传值 方法描述:在通知接收方需要注册通知,并指定接收到通知后进行的操作:而在通知发送方则在需要传递数据时发送通知就OK了.通知的操作都是通过NSNotificationCenter来完成的 ...

  3. UI一揽子计划 8 (UINavigationController 、界面通信 、NSTimer  、NSUserDefaults)

    一.UINavigationController       //  创建一个导航控制器     // 创建一个控制器作为根控制器 去管理     RootViewController *rootVC ...

  4. iOS 代理反向传值

    iOS 代理反向传值 在上篇博客 iOS代理协议 中,侧重解析了委托代理协议的概念等,本文将侧重于它们在开发中的应用. 假如我们有一个需求如下:界面A上面有一个button.一个label.从界面A跳 ...

  5. 蓝牙接收苹果手机通知 ANCS协议分析

    转载,请注明出处:http://www.cnblogs.com/alexcai/p/4321514.html 综述 现在有许多蓝牙手表.手环都能接收苹果iphone手机的系统通知,那这是怎么实现的呢? ...

  6. ATT Protocol 属性协议

    1.背景 接触了,就必须留下点痕迹. 1.1 参考资料 Bluetooth ATT介绍  https://www.cnblogs.com/hzl6255/p/4141505.html ble v4.2 ...

  7. iOS开发之本地通知UILocalNotification

    本地通知是UILocalNotification的实例,主要有三类属性: scheduled time:时间周期,用来指定iOS系统发送通知的日期和时间: notification type:通知类型 ...

  8. 《iOS 9 开发指南》——第6章,第6.7节iOS 9控件的属性

    本节书摘来自异步社区<iOS 9 开发指南>一书中的第6章,第6.7节 iOS 9控件的属性,作者 管蕾,更多章节内容可以访问云栖社区"异步社区"公众号查看 6.7 i ...

  9. 苹果iOS 16将改进通知 添加新的健康追踪功能

    4月11日消息,据国外媒体报道,代号为"Sydney"的iOS16将在6月的WWDC上推出预览版,分析师称,总体来说不会有太大的设计改变,主要将对通知和新健康追踪功能进行改进. 据 ...

最新文章

  1. QuestMobile 2017年中国移动互联网年度报告
  2. stm32阶段总结笔记一
  3. 为什么我要放弃javaScript数据结构与算法(第二章)—— 数组
  4. VTK:可视化之NamedColors
  5. python语言用什么关键字来声明一个类_Python语言和标准库(第三章:类和对象)...
  6. 1055 - Expression #1 of ORDER BY clause is not in GROUP BY clause and contai
  7. 如何测试连接MsSQL数据库-------UDL文件
  8. C语言 extern
  9. .ssh文件夹在哪里_【TOOLS】本地利用ssh远程连接服务器并启用远程服务器的jupyter lab并配置好anaconda的环境...
  10. Android异步加载全解析之引入二级缓存
  11. 用计算机算非,在线计算器上的与、或、非、异或等逻辑运算键如何使用?
  12. Vue之Promise
  13. 从函数式接口到lambda表达式
  14. 数字通信原理的几个理解
  15. 全国计算机软考机试试题,软考机试试题.doc
  16. html方框里打勾,word里如何在□里打√!框框里打勾!!!!!
  17. vue 跳转到指定路由地址 (可附带参数)
  18. BSCI社会责任验厂对员工考勤和工资的标准和要求
  19. python股票策略_用Python编程彼得林奇PEG价值选股策略
  20. Windflowers(歌词)

热门文章

  1. ___new__方法和__init__方法的区别
  2. [IOI2011]Race
  3. Shell命令-文件及内容处理之split、paste
  4. CSS实例:图片导航块
  5. Python笔记——Django路由系统
  6. jQuery 插件-(初体验一)
  7. M2第三天DailyScrum——PM(李忠)
  8. java生成可执行文件的方法总结
  9. 2021巢湖第一中学高考成绩查询,2021年巢湖高中录取分数线是多少及高中排名榜...
  10. starting mysql. success!_启动mysql报错解决问题过程