天天看點

iOS筆記—Notification

Notification,一對多的情況下可以使用

//
//  ViewController.m
//   通知
//
//  Created by hhg on 15/9/28.
//  Copyright (c) 2015年 hhg. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    UIButton *button =  [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(, , , );
    button.backgroundColor = [UIColor blueColor];
    [self.view addSubview:button];
    [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];

    center = [NSNotificationCenter defaultCenter];
    [center addObserver:self selector:@selector(centerFunc:) name:@"Notification" object:nil];

}
-(void)buttonClick:(UIButton *)button {
    [center postNotificationName:@"Notification" object:nil];
}

-(void)centerFunc:(NSNotificationCenter *)centerPart {

    if ([[centerPart valueForKey:@"name"] isEqualToString:@"Notification"]) {
        NSLog(@"Notification is action");
    } else {
        NSLog(@"不比對");
    }

}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end
           

繼續閱讀