简体   繁体   English

IBActions 是否在主队列上触发?

[英]Are IBActions fired on main queue?

I tried to search this out, but kinda stuck in this question.我试图搜索这个,但有点卡在这个问题上。 All guides about UI say, that all UI stuff should be on GCD main thread, but no one says about inner implementation of IBActions.所有关于 UI 的指南都说,所有 UI 的东西都应该在 GCD 主线程上,但没有人说 IBActions 的内部实现。

So, are IBActions fired on GCD main thread or not?那么,IBActions 是否在 GCD 主线程上触发?

Yes, and you can test it by yourself using NSLog(@"is main thread? %d", [NSThread isMainThread]);是的,您可以使用NSLog(@"is main thread? %d", [NSThread isMainThread]); You can also use the debugger and left view to know about what thread is been executed your code.您还可以使用调试器和左视图来了解您的代码执行了哪个线程。

It is safe to assume that @IBAction s are only called on the main thread by Apple's code under the hood.可以安全地假设@IBAction s 只在主线程上被 Apple 的代码在引擎盖下调用。 Apple's documentation :苹果的文档

Important : Use UIKit classes only from your app's main thread or main dispatch queue, unless otherwise indicated.重要提示:除非另有说明,否则只能使用应用程序主线程或主调度队列中的 UIKit 类。 This restriction particularly applies to classes derived from UIResponder or that involve manipulating your app's user interface in any way.此限制特别适用于派生自 UIResponder 或涉及以任何方式操作应用程序用户界面的类。

Note : it is entirely possible for your code to call an @IBAction 's method on a background thread.注意:您的代码完全有可能在后台线程上调用@IBAction的方法。 That is why I make that distinction.这就是我做出这种区分的原因。

Note : If you are here because an error on an IBOutlet, Make your IBOutlets strong (not weak) to avoid accessing dereferenced IBOutlets in some cases.注意:如果您因为 IBOutlet 上的错误而来到这里,请使您的 IBOutlets 强(而不是弱)以避免在某些情况下访问取消引用的 IBOutlets。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM