Why is UIResponder inputAccessoryView atomic?
-
04-03-2021 - |
質問
I'm using inputAccessoryView
for one of my custom subclasses of UIViewController
, which subclasses UIResponder
.
The Apple Developer Class Reference for -[UIResponder inputAccessoryView]
states:
Subclasses that want to attach custom controls to either a system-supplied input view (such as the keyboard) or a custom input view (one you provide in the
inputView
property) should redeclare [theinputAccessoryView
] property asreadwrite
and use it to manage their custom accessory view.
- After redeclaring
inputAccessoryView
, must I then@synthesize
it? Doing so seemed to be the only way to get it to compile, but I want to use Apple'sinputAccessoryView
ivar, not synthesize my own. - Can I redeclare
inputAccessoryView
asnonatomic
? - If I cannot redeclare
inputAccessoryView
asnonatomic
, then must I always to accessinputAccessoryView
withself.inputAccessoryView
, i.e., via the property instead of directly accessing the ivar, in order to preserve thread safety?
解決
Yes, you'll have to provide a setter for the property yourself, unless UIResponder exposes a
setInputAccessoryView:
method. If you had access to the underlying ivar, you could just write a setInputAccessoryView: method yourself which sets it. In this case, though, you don't have access to it, so you'll have to create one yourself.I don't think so; this wouldn't be compatible with the superclass declaration.
I don't understand why this property is not
nonatomic
, since calling (nearly) anything in UIKit from a non-main thread is invalid. In particular, this property is aUIView
, and there's no way for the setter to retain and releaseUIView
s on a non-main thread safely.In other words, if the setter is being called from a background thread, the code is broken either way. If it's only called from the main thread, you can access the ivar directly. So, there's no reason you can't access the ivar directly.