iOS-通知
2015-12-07 17:34
381 查看
<p class="p1"><span class="s1"> </span><span class="s2">/**</span></p><p class="p2"><span class="s3"> * </span><span class="s2">通知中心</span><span class="s3"> - </span><span class="s2">是观察者模式的一种体现</span></p><p class="p1"><span class="s2"> * 1</span><span class="s4">对多的关系</span></p><p class="p2"><span class="s3"> </span><span class="s2">三种操作:订阅通知,取消通知,发送通知</span></p><p class="p2"><span class="s3"> </span><span class="s2">凡是订阅通知的在有人发送对应名字通知时都可以收到通知进行下一步处理</span></p><p class="p1"><span class="s2"> *</span></p><p class="p1"><span class="s2"> */</span></p>
#import "ViewController.h" #import "LeftViewController.h" #import "RightViewController.h" @interface ViewController () @property (nonatomic, strong) LeftViewController *leftVC; @property (nonatomic, strong) RightViewController *rightVC; @property (nonatomic, strong) UIButton *pushLeft; @property (nonatomic, strong) UIButton *pushRight; @property (nonatomic, strong) UIButton *postNotButton; @end #define kPostNotButtonWidth [UIScreen mainScreen].bounds.size.width - 24 #define kPostNotButtonHeight 50 #define kPostLeftButtonY 200 #define kPostLeftButtonWidth 100 #define kPostLeftButtonHeight 50 static const NSInteger kPostNotButtonX = 12; static const NSInteger kPostNotButtonY = 64 + 20; @implementation ViewController #pragma mark - getter - (LeftViewController *)leftVC { if (!_leftVC) { _leftVC = [[LeftViewController alloc] init]; _leftVC.view.backgroundColor = [UIColor redColor]; } return _leftVC; } - (RightViewController *)rightVC { if (!_rightVC) { _rightVC = [[RightViewController alloc] init]; _rightVC.view.backgroundColor = [UIColor blueColor]; } return _rightVC; } - (UIButton *)postNotButton { if (!_postNotButton) { _postNotButton = [UIButton buttonWithType:UIButtonTypeCustom]; _postNotButton.frame = CGRectMake(kPostNotButtonX, kPostNotButtonY, kPostNotButtonWidth, kPostNotButtonHeight); _postNotButton.backgroundColor = [UIColor redColor]; [_postNotButton addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside]; [_postNotButton setTitle:@"发送通知" forState:UIControlStateNormal]; _postNotButton.titleLabel.font = [UIFont systemFontOfSize:15]; _postNotButton.titleLabel.textAlignment = NSTextAlignmentCenter; _postNotButton.tag = 1; // _postNotButton.titleLabel.font = [UIFont fontNamesForFamilyName:@""]; [self.view addSubview:_postNotButton]; } return _postNotButton; } - (UIButton *)pushLeft { if (!_pushLeft) { _pushLeft = [UIButton buttonWithType:UIButtonTypeCustom]; _pushLeft.frame = CGRectMake(kPostNotButtonX, kPostLeftButtonY, kPostLeftButtonWidth, kPostLeftButtonHeight); [_pushLeft setTitle:@"跳转左侧" forState:UIControlStateNormal]; [_pushLeft addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside]; _pushLeft.tag = 2; [self.view addSubview:_pushLeft]; } return _pushLeft; } - (UIButton *)pushRight { if (!_pushRight) { _pushRight = [UIButton buttonWithType:UIButtonTypeCustom]; _pushRight.frame = CGRectMake(kPostNotButtonX + 120, kPostLeftButtonY, kPostLeftButtonWidth, kPostLeftButtonHeight); [_pushRight addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside]; [_pushRight setTitle:@"跳转右侧" forState:UIControlStateNormal]; _pushRight.tag = 3; [self.view addSubview:_pushRight]; } return _pushRight; } #pragma mark - Life Circle - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. self.view.backgroundColor = [UIColor orangeColor]; self.title = @"通知"; //订阅 通知 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeColor:) name:@"changeColor" object:nil]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (instancetype)init { self = [super init]; if (self) { } return self; } - (void)changeColor:(NSNotification *)not { UIColor *color = not.object; self.view.backgroundColor = color; } - (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { [self leftVC]; [self rightVC]; [self postNotButton]; [self pushLeft]; [self pushRight]; } return self; } #pragma mark - Action - (void)btnClick:(UIButton *)btn { if (btn.tag == 1) { //post NotFication [self alertView]; [[NSNotificationCenter defaultCenter] postNotificationName:@"changeColor" object:[UIColor cyanColor]]; } else if (btn.tag == 2) { [self.navigationController pushViewController:_leftVC animated:YES]; } else if (btn.tag == 3) { [self.navigationController pushViewController:_rightVC animated:YES]; } else { //do nothing... } } - (void)dealloc { //移除通知 [[NSNotificationCenter defaultCenter] removeObserver:self]; } - (void)alertView { UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"发送通知成功" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil]; [alert show]; } @end
#import "LeftViewController.h" @interface LeftViewController() @end @implementation LeftViewController - (void)viewDidLoad { //注册通知 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeColor:) name:@"changeColor" object:nil]; } - (void)changeColor:(NSNotification *)not { UIColor *color = not.object; self.view.backgroundColor = color; } @end
#import "RightViewController.h" @interface RightViewController() @end @implementation RightViewController - (void)viewDidLoad { //注册通知 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeColor:) name:@"changeColor" object:nil]; } - (void)changeColor:(NSNotification *)not { UIColor *color = not.object; self.view.backgroundColor = color; } @end
相关文章推荐
- iOS 异步图片加载优化与常用开源库分析
- iOS开发ARC内存管理技术要点
- IOS使用soap。
- CoreImage
- 详细讲述iOS自定义转场
- iOS开发 3D-touch使用
- 如何设计一个 iOS 控件?(iOS 控件完全解析)
- Ios和tvos按需请求资源简介
- ios开发之升级Xcode后插件失效解决方法
- iOS图片处理
- 更新更新!重磅出击!【学院官方整理】玩转iOS 8开发,必学的33个经典视频教程
- IOS界面生命周期
- iOS开发之 类目,延展,协议
- iOS开发中MD5的精髓
- Html判断客户端使用的Android还是Ios
- iOS启动动画效果实现
- iOS开发之单例,如何正确的创建单例。
- IOS学习开发路线图
- ios应用内支付过程(使用苹果原生支付方式
- IOS 后台运行申请更多的时间