iOS block 代理 通知方法总结
2017-03-28 18:11
344 查看
一.block
先声明block
<1>声明block
typedef void(^myBlock)(NSString *name);
//<2>声明一个block属性
@property (nonatomic,
copy) myBlock block;
//<3>调用block、传值
if (self.block) {
self.block(@"123");
}
//<4>实现block
//a:NSString *name
代表传过来的值
next.block = ^(NSString *name){
//<5>实现
NSLog(@"%@",name);
self.view.backgroundColor = [UIColor
cyanColor];
};
二.代理
@protocol NextViewControllerDelegate <NSObject>
-(void)nextViewControllerSendData;
@end
@property (nonatomic,weak)
id<NextViewControllerDelegate> delegate;
if ([self.delegate respondsToSelector:@selector(nextViewControllerSendData)]) {
//代理有实现方法
[self.delegate nextViewControllerSendData];
}
//遵守协议
@interface ViewController ()<NextViewControllerDelegate>
next.delegate = self;
-(void)nextViewControllerSendData{
NSLog(@"nextViewControllerSendData");
}
三通知
//1.发送通知
[[NSNotificationCenter
defaultCenter]postNotificationName:@"notification"
object:nil
userInfo:@{@"key":[NSString
stringWithFormat:@"%ld",_number]}];
//2.接收通知
[[NSNotificationCenter
defaultCenter] addObserver:self
selector:@selector(addNumber:)
name:@"notification"
object:nil];
- (void)addNumber:(NSNotification *)noti
{
UILabel * label = (id)[self.view
viewWithTag:10];
label.text = [NSString
stringWithFormat:@"%@",[noti.userInfo
objectForKey:@"key"]];
}
// 3.根据观察者
移除所有通知
[[NSNotificationCenter
defaultCenter] removeObserver:self];
先声明block
<1>声明block
typedef void(^myBlock)(NSString *name);
//<2>声明一个block属性
@property (nonatomic,
copy) myBlock block;
//<3>调用block、传值
if (self.block) {
self.block(@"123");
}
//<4>实现block
//a:NSString *name
代表传过来的值
next.block = ^(NSString *name){
//<5>实现
NSLog(@"%@",name);
self.view.backgroundColor = [UIColor
cyanColor];
};
二.代理
@protocol NextViewControllerDelegate <NSObject>
-(void)nextViewControllerSendData;
@end
@property (nonatomic,weak)
id<NextViewControllerDelegate> delegate;
if ([self.delegate respondsToSelector:@selector(nextViewControllerSendData)]) {
//代理有实现方法
[self.delegate nextViewControllerSendData];
}
//遵守协议
@interface ViewController ()<NextViewControllerDelegate>
next.delegate = self;
-(void)nextViewControllerSendData{
NSLog(@"nextViewControllerSendData");
}
三通知
//1.发送通知
[[NSNotificationCenter
defaultCenter]postNotificationName:@"notification"
object:nil
userInfo:@{@"key":[NSString
stringWithFormat:@"%ld",_number]}];
//2.接收通知
[[NSNotificationCenter
defaultCenter] addObserver:self
selector:@selector(addNumber:)
name:@"notification"
object:nil];
- (void)addNumber:(NSNotification *)noti
{
UILabel * label = (id)[self.view
viewWithTag:10];
label.text = [NSString
stringWithFormat:@"%@",[noti.userInfo
objectForKey:@"key"]];
}
// 3.根据观察者
移除所有通知
[[NSNotificationCenter
defaultCenter] removeObserver:self];
相关文章推荐
- iOS 传值方法(属性传值、代理传值、Block、通知、单例)
- 2017iOS面试经典总结(1)--- iOS 代理,通知,Block
- IOS回调机制——代理,通知中心以及Block
- iOS UIApplication的代理方法精华总结
- iOS 页面间几种传值方式(属性,代理,block,单例,通知)
- IOS中5种传值方式详解(属性、Block代码块、代理、单例、通知)
- iOS 页面间几种传值方式(属性,代理,block,单例,通知)
- iOS开发-UIScrollView代理方法总结
- iOS 之单例,代理,通知,KVO,Block全能解析
- iOS传值方式:属性,代理,block,单例,通知
- 页面之间传值方式的总结,五种方式,通知,block,代理,单例,NSUERDEFALUT,
- iOS 页面间几种传值方式(属性,代理,block,单例,通知)
- iOS 页面间几种传值方式(属性,代理,block,单例,通知)
- iOS 页面间几种传值方式(属性,代理,block,单例,通知)
- IOS回调机制——代理,通知中心以及Block
- iOS_21团购_通过block对请求工具类的代理方法进行二次封装
- iOS 页面间几种传值方式(属性,代理,block,单例,通知)
- iOS 几种传值方式(属性,代理,block,通知,本地存储,单例类)附Demo
- 代理,block,通知,对于数据回传的不同方法
- iOS中UITableView使用总结和 iOS UITableView代理方法详解