iOS-通知
来源:互联网 发布:模糊算法 疲劳识别 编辑:程序博客网 时间:2024/05/22 02:10
<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 50static 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
0 0
- ios 通知
- IOS 通知
- iOS通知
- iOS 通知
- IOS通知
- IOS 通知
- iOS-通知
- iOS通知
- ios 通知
- iOS 【通知】
- iOS通知
- IOS 通知
- ios 通知
- iOS 通知
- iOS - 通知
- ios 通知
- iOS 本地通知 远程通知
- iOS本地通知 远程通知
- poj 2063 Investment 贪心+完全背包
- 主键和唯一索引的区别
- echo换行
- 博客、把一些开发文章转发整理
- Android TextView两端对齐
- iOS-通知
- LoadRunner用户行为模拟器 《第三篇》
- L1 L2 LASSO问题-难得的好文
- android(40)(撕衣服,bitmap的使用)
- Git分支管理策略
- Android四大组件----BroadcastReceiver
- Android 计步器开发
- 解决win10/win8/8.1 64位操作系统MT65xx preloader线刷驱动无法安装
- Android Studio的一些报错问题