您的位置:首页 > 移动开发 > IOS开发

iOS开发之数据传递(二)

2015-12-11 15:19 316 查看
前面讲到数据传递的方式有很多,现在我们继续学习数据传递的其他方式:代理传值和通知传值

代理传值

当对象A想利用某个数据去做一些事情,但是A不亲自去做,而是通知它的代理对象B去做,这就要把数据传给B,让B去做。也就涉及到了数据传递。

并不是所有的对象都能成为A的代理。只有遵循A的协议的对象才能成为A的代理。

协议就是一套接口,成为A的代理就要实现这套接口。

下面通过一个例子来解释:

一个HXPassenger对象要去开车,以velocity的速度开。但是他不是自己开车,而是让他的代理对象HXDriver来开,也是以velocity的速度。

首先有一个HXPassenger类,HXPassenger.h

#import <Foundation/Foundation.h>

@protocol HXPassengerDelegate <NSObject>

@optional
// 协议(要想给我开车,必须实现(遵循)这个协议)
- (void)canDrive:(CGFloat)velocity;

@end

@interface HXPassenger : NSObject
// 代理对象
@property (nonatomic, weak) id<HXPassengerDelegate> delegate;

- (void)drive;

@end

HXPassenger.m

#import "HXPassenger.h"

@implementation HXPassenger

- (void)drive {
CGFloat velocity = 10.8;
NSLog(@"%@-人要开车,以%f速度", self, velocity);

// 我要开车,通知代理(如果有代理(即实现协议方法),代理就实现该协议方法,自动跳到代理对象中去实现该方法)
if ([self.delegate respondsToSelector:@selector(canDrive:)]) {
[self.delegate canDrive:velocity];
}
}

@end

再创建一个HXDriver类,成为HXPassenger的代理,HXDriver.h

#import <Foundation/Foundation.h>
#import "HXPassenger.h"

@interface HXDriver : NSObject<<span style="color:#FF0000;">HXPassengerDelegate</span>>

@end


HXDriver.m

#import "HXDriver.h"
@implementation HXDriver

// 我是你的代理,我实现(遵循)了这个协议
- (void)canDrive:(CGFloat)velocity {
NSLog(@"%@-我是你的司机,我来开,以%f速度", self, velocity);
}

@end

在main方法中如下:

#import <Foundation/Foundation.h>
#import "HXPassenger.h"
#import "HXDriver.h"

int main(int argc, const char * argv[]) {
@autoreleasepool {

HXPassenger *passeger = [[HXPassenger alloc] init];
NSLog(@"人-%@", passeger);

HXDriver *driver = [[HXDriver alloc] init];
NSLog(@"司机-%@", driver);
// 设置driver为passeger的代理
passeger.delegate = driver;

// passeger要开车
[passeger drive];
}
return 0;
}

运行结果:



passeger不仅让driver替他开车,还把passeger想以多少速度的值也传递给了driver。

原理图如下:



Protocol(协议):Object-C是不支持多继承的,所以很多时候都是用Protocol(协议)来代替。Protocol(协议)只能定义公用的一套接口,但不能提供具体的实现方法。也就是说,它只告诉你要做什么,但具体怎么做,它不关心。

当一个类要使用某一个Protocol(协议)时,都必须要遵守协议。比如有些必要实现的方法,你没有去实现,那么编译器就会报警告,来提醒你没有遵守××协议。注意,我这里说的是警告,而不是错误。对的,就算你不实现那些“必要实现”的方法,程序也是能运行的,只不过多了些警告。

通知传值

通知跟代理的功能是一样的,都是传值,调方法,但是我个人觉得通知比代理还是要简便的,而且功能更强大。

那么通知和代理的区别是什么呢?

代理是一对一传值,但是通知可以一对多或多对多,好了不多说了给大家具体讲一下。

每一个应用程序都有一个通知中心(NSNotificationCenter)实例,专门负责协助不同对象之间的消息通信,任何一个对象都可以向通知中心发布通知(NSNotification),描述自己在做什么。其他感兴趣的对象(Observer)可以申请在某个特定通知发布时(或在某个特定的对象发布通知时)收到这个通知。

一个完整的通知一般包含3个属性:

- (NSString *)name; // 通知的名称

- (id)object; // 通知发布者(是谁要发布通知)

- (NSDictionary *)userInfo; // 一些额外的信息(通知发布者传递给通知接收者的信息内容)

初始化某个通知(NSNotification)对象:

+(instancetype)notificationWithName:(NSString *)aName object:(id)anObject;

+(instancetype)notificationWithName:(NSString *)aName object:(id)anObjectuserInfo:(NSDictionary *)aUserInfo;

- (instancetype)initWithName:(NSString*)name object:(id)object userInfo:(NSDictionary *)userInfo;

初始化完该通知对象,也就向通知中心发布该通知,说明自己在干什么了,那么如何发布该通知呢?通知中心(NSNotificationCenter)提供了相应的方法来帮助发布通知:

- (void)postNotification:(NSNotification*)notification;

//发布一个notification通知,可在notification对象中设置通知的名称、通知发布者、额外信息等

- (void)postNotificationName:(NSString*)aName object:(id)anObject;

//发布一个名称为aName的通知,anObject为这个通知的发布者

- (void)postNotificationName:(NSString*)aName object:(id)anObject userInfo:(NSDictionary *)aUserInfo;

//发布一个名称为aName的通知,anObject为这个通知的发布者,aUserInfo为额外信息

现在通知中心已经发布了某个通知,那么别人如何来获取(监听)这个通知,注册监听通知器,通知中心(NSNotificationCenter)提供了方法来注册某个通知的监听器(Observer)

- (void)addObserver:(id)observerselector:(SEL)aSelector name:(NSString *)aName object:(id)anObject;

//observer:监听器,即谁要接收这个通知

//aSelector:收到通知后,回调监听器的这个方法,并且把通知对象当做参数传入

//aName:通知的名称。如果为nil,那么无论通知的名称是什么,监听器都能收到这个通知

//anObject:通知发布者。如果为anObject和aName都为nil,监听器都收到所有的通知

通知中心不会保留(retain)监听器对象,在通知中心注册过的对象,必须在该对象释放前取消注册。否则,当相应的通知再次出现时,通知中心仍然会向该监听器发送消息。因为相应的监听器对象已经被释放了,所以可能会导致应用崩溃

//通知中心提供了相应的方法来<span style="color:#FF0000;">取消注册监听器</span>

- (void)removeObserver:(id)observer;

- (void)removeObserver:(id)observername:(NSString *)aName object:(id)anObject;

//一般在监听器销毁之前取消注册(如在监听器中加入下列代码):
- (void)dealloc {

//[super dealloc];  非ARC中需要调用此句

[[NSNotificationCenter defaultCenter] removeObserver:self];

}
当应用出发某个事件,系统也会发出一些通知,这些属于系统自带的通知:下面列举一些系统自带的通知。

UIDevice对象会不间断地发布一些通知,下列是UIDevice对象所发布通知的名称常量:

UIDeviceOrientationDidChangeNotification //设备旋转

UIDeviceBatteryStateDidChangeNotification// 电池状态改变

UIDeviceBatteryLevelDidChangeNotification// 电池电量改变

UIDeviceProximityStateDidChangeNotification// 近距离传感器(比如设备贴近了使用者的脸部)

键盘状态改变的时候,系统会发出一些特定的通知

UIKeyboardWillShowNotification // 键盘即将显示

UIKeyboardDidShowNotification // 键盘显示完毕

UIKeyboardWillHideNotification // 键盘即将隐藏

UIKeyboardDidHideNotification // 键盘隐藏完毕

UIKeyboardWillChangeFrameNotification // 键盘的位置尺寸即将发生改变

UIKeyboardDidChangeFrameNotification // 键盘的位置尺寸改变完毕

//系统发出键盘通知时,会附带一下跟键盘有关的额外信息(字典),字典常见的key如下:

UIKeyboardFrameBeginUserInfoKey // 键盘刚开始的frame

UIKeyboardFrameEndUserInfoKey // 键盘最终的frame(动画执行完毕后)

UIKeyboardAnimationDurationUserInfoKey // 键盘动画的时间

UIKeyboardAnimationCurveUserInfoKey // 键盘动画的执行节奏(快慢)

下面写一个关于系统通知的实例:(键盘通知)

- (void)viewDidLoad {
[super viewDidLoad];

// 向文本框输入内容
// 当一点击文本框的时候,默认情况下手机会自动弹出键盘
// 当键盘状态改变的时候,系统会发出一些通知,来通知应用键盘的状态改变

// 由于键盘通知是系统自动发出的,不需要手动发布键盘通知
// 所以直接向通知中心注册键盘通知监听器
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
}

/**
*  键盘通知的监听方法
*
*  @param notification 键盘的状态信息
*/
- (void)keyboardWillShow:(NSNotification *)notification {

NSDictionary *dict = notification.userInfo;

NSLog(@"keyboardWillShow --- %@", dict);
}
- (void)dealloc {
// 移除self身上的所有通知
[[NSNotificationCenter defaultCenter] removeObserver:self];
}

运行结果:(当点击文本框的时候,键盘刚要弹出的时候,会发出UIKeyboardWillShowNotification通知,并把键盘信息传递给了控制器)



除了系统可以发布通知外,我们也可以发布自己定义的通知,下面就演示发布自己的通知:(数据从下一个页面传递给上一个页面)

ViewController.m(上一个)

#import "ViewController.h"
#import "NextViewController.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UITextField *textField;
- (IBAction)clickBtn;

@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];
// 向通知中心注册通知监听器
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(acceptValue:) name:@"123" object:nil];
}

// 监听到通知后的回调方法
- (void)acceptValue:(NSNotification *)noti {
NSLog(@"acceptValue - %@", noti.userInfo[@"text"]);
self.textField.text = noti.userInfo[@"text"];
}

// 点击按钮跳转到下一个页面
- (IBAction)clickBtn {

NextViewController *vc = [[NextViewController alloc] init];
UINavigationController *na = [[UINavigationController alloc] initWithRootViewController:vc];

[self presentViewController:na animated:YES completion:nil];
}
- (void)dealloc {
    // 移除self身上的所有通知
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
@end

NextViewController.m(下一个)

#import "NextViewController.h"

@interface NextViewController ()

@end

@implementation NextViewController

- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor redColor];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
NSDictionary *dict = @{@"text" : @"什么玩意"};
// 向通知中心注册通知
NSNotification *noti = [NSNotification notificationWithName:@"123" object:nil userInfo:dict];
// 通知中心发布通知
[[NSNotificationCenter defaultCenter] postNotification:noti];

[self dismissViewControllerAnimated:YES completion:nil];
}

@end

数据已经传递到上一个页面的textField中,还有打印结果:







上面的通知名就是我们自己定义的@”TextField”。

通知示意图:



到这里数据传递的方法已经都介绍完了,这些方法各有优缺点,使用的时候要看场景进行选择。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: