前言
最近有個小伙伴到喜馬拉雅去面試,面試官問他一個問題就是“通知中心是同步操作還是異步操作赡茸?”缎脾,小伙伴回答成異步了,然后就是回家等消息占卧,然后就是沒有然后了遗菠。。华蜒。
我先舉幾個小的列子給大家瞅瞅:
(void)sentValueBtnClick:(UIButton *)button{
NSLog(@"發(fā)送通知");
NSDictionary *dict = @{@"myValue":@"ZFJ通知傳值"};
[[NSNotificationCenter defaultCenter] postNotification:[NSNotification notificationWithName:@"KPassValue" object:nil userInfo:dict]];
}-
(void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
_textField.layer.borderColor = [UIColor redColor].CGColor;
_textField.layer.borderWidth = 1.0;[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(passValue:) name:@"KPassValue" object:nil];
} (void)passValue:(NSNotification *)text{
NSString *valueStr = text.userInfo[@"myValue"];
NSLog(@"收到值:%@",valueStr);
sleep(3);
self.textField.text = valueStr;
NSLog(@"通知賦值完畢");