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

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: