NSNotification调用机制

本文深入探讨了Objective-C与Swift两种编程语言在iOS开发领域的应用,包括它们各自的优势、特点以及如何在实际项目中进行选择与使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

发送NSNotification的时候是同步机制,接受通知的方法执行完成后才会向下运行。示例代码如下:


- (void)viewDidLoad {

    [super viewDidLoad];

    

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(logOne) name:@"notificationOne"object:nil];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(logTwo) name:@"notificationTwo"object:nil];

    

    NSLog(@"notificationOne b");


    [NSThread detachNewThreadSelector:@selector(notificationOne) toTarget:self withObject:nil];

    NSLog(@"notificationOne e");

    sleep(1);

    NSLog(@"notificationTwo b");


    [NSThread detachNewThreadSelector:@selector(notificationTwo) toTarget:self withObject:nil];

    NSLog(@"notificationTwo e");


    // Do any additional setup after loading the view, typically from a nib.

}


-(void)notificationOne{

    [[NSNotificationCenter  defaultCenter] postNotificationName:@"notificationOne" object:nil];

}


-(void)notificationTwo{

    [[NSNotificationCenter  defaultCenter] postNotificationName:@"notificationTwo" object:nil];

}


-(void)logOne{

    NSLog(@"logOne b");

    sleep(10);

    NSLog(@"logOne e");

    

}


-(void)logTwo{

    NSLog(@"logTwo b");

    sleep(1);

    NSLog(@"logTwo e");

    

}



2016-07-01 11:56:11.789 NotificationPostTest[854:20877] notificationOne b

2016-07-01 11:56:11.790 NotificationPostTest[854:20877] notificationOne e

2016-07-01 11:56:11.790 NotificationPostTest[854:20960] logOne b

2016-07-01 11:56:12.791 NotificationPostTest[854:20877] notificationTwo b

2016-07-01 11:56:12.792 NotificationPostTest[854:20877] notificationTwo e

2016-07-01 11:56:12.792 NotificationPostTest[854:20966] logTwo b

2016-07-01 11:56:13.797 NotificationPostTest[854:20966] logTwo e

2016-07-01 11:56:21.793 NotificationPostTest[854:20960] logOne e


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值