我有UIViewController的简单子类(下面的代码)。如果我附加inputAccessoryView,我的viewcontroller永远不会被释放。如果我没有在viewDidLoad中设置inputAccessoryView,则按预期调用dealloc。
我错过了什么吗?
@interface IMTestViewController : UIViewController
@property (nonatomic, strong) UIView *messageInputView;
@property(nonatomic, readwrite, strong) UIView *inputAccessoryView;
@end
@implementation IMTestViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)dealloc
{
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.inputAccessoryView = self.messageInputView;
}
- (BOOL)canBecomeFirstResponder
{
return YES;
}
- (UIView *)messageInputView
{
if (_messageInputView == nil)
{
_messageInputView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 45)];
_messageInputView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
}
return _messageInputView;
}
@end
我已经没想完了。谢谢。
对我来说很遗憾@ rdelmar的回答没有用。花了一些时间试图解决它,我发现这篇文章:http://derpturkey.com/uitextfield-docked-like-ios-messenger/
我的目标是即使键盘没有,也可以看到输入附件视图,就像在所有IM应用程序中一样。我之前将我的UIViewController
自定义类子类化为允许它成为第一响应者并将我的自定义子视图作为inputAccessoryView
返回。这阻止了视图控制器被解除分配。现在我将控制器的视图子类化,以实现与上面链接中建议的相同,一切似乎都正常。
编辑:经过一些更多的测试,我可以确认自定义UIView被解除分配。
编辑2:唯一的缺点是你不能使键盘出现在viewWillAppear
中,inputAccessoryView
尚未添加到视图层次结构中并且不能成为第一响应者。
这个问题相当陈旧,但我在2019年尝试在iOS 12中使用inputAccessoryView时遇到了它。
解除分配问题至今仍然存在,并且在dvkch's answer提到的文章中提出的第一个解决方案也不起作用。文章中的第二个解决方案(涉及动画)工作量太大,当用户通过带有scrollView.keyboardDismissMode = .interactive
的UIScrollView以交互方式解除键盘时效果不佳。
我能想到的最好的方法就是在UITextField
上将第一响应者UITextView
或inputAccessoryView
nil
设置为viewDidDisappear
。这完全消除了内存泄漏,似乎没有任何副作用或缺点。
所以这是一个完整的Swift 4.2示例:
class MyViewController: UIViewController {
/// You could also add your text field or text view programmatically,
/// but let's say it's coming from a .xib for now...
@IBOutlet private weak var myTextField: UITextField!
/// This is required for the inputAccessoryView to work.
override internal var canBecomeFirstResponder: Bool {
return true
}
/// Here's a nice empty red view that will be used as an
/// input accessory.
private lazy var accessoryView: UIView = {
let accessoryView = UIView()
accessoryView.backgroundColor = UIColor.red
accessoryView.frame.size = CGSize(
width: view.frame.size.width,
height: 45
)
return accessoryView
} ()
override var inputAccessoryView: UIView? {
return accessoryView
}
/// This is required to avoid leaking the `inputAccessoryView`
/// when the keyboard is open and the `UIViewController`
/// is deallocated.
override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
myTextField.inputAccessoryView = nil
}
}