以下代码之间有什么区别
1)
[NSThread detachNewThreadSelector:@selector(myFunction) toTarget:self withObject:thename];
Run Code Online (Sandbox Code Playgroud)
2)
[NSTimer scheduledTimerWithTimeInterval:1.0
target:self
selector:@selector(myFunction:)
userInfo:nil
repeats:NO];
Run Code Online (Sandbox Code Playgroud)
3)
[self performSelector:@selector(myFunction) withObject:nil afterDelay:myDelay];
Run Code Online (Sandbox Code Playgroud)
4)
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(myFunction:) name:thename object:nil];
Run Code Online (Sandbox Code Playgroud)
在单独的线程中执行选择器.如果要在被调用函数中自动释放对象,则需要设置自己的自动释放池.您无法直接使用GUI,但如果该功能需要很长时间才能完成,则不会阻止它.可能是更好的使用写入performSelectorInBackground:的NSObject.
在主线程上延迟后运行选择器.不需要自动释放池(您使用的是默认值),您可以直接使用GUI,但如果函数需要很长时间才能完成,则会阻止它.
非常喜欢2.
完全不同的东西,请参阅NSNotificationCenter的文档.您告诉默认通知中心您要接收(=任何对象)thename发送的所有名称通知nil.当发布此类通知时,通知中心将调用myFunction并向其传递NSNotification描述该事件的实例.
我希望我把一切都搞定,前三点有点棘手.
| 归档时间: |
|
| 查看次数: |
624 次 |
| 最近记录: |