“self.delegate = self”无法在使用 ARC 的 iOS 上工作
我正在开发一个启用了 ARC 的 iOS SDK 4 项目。
我的类 MyTextView
(从带有 UITextViewDelegate
协议的 UITextView 派生)实现以下静态方法:
+ (void)showInViewController:(UIViewController*)viewController
{
MyTextView *textEdit = [[MyTextView alloc] init];
textEdit.delegate = textEdit;
[viewController.view addSubview:textEdit];
// Show the keyboard
[textEdit becomeFirstResponder];
}
在我的一个视图控制器中,我调用以下内容:
[MyTextView showInViewController:self]
这会崩溃并发出警告:无法恢复先前选择的帧。 在 becomeFirstResponder
上。看起来像由于某些循环而导致一些堆栈相关的崩溃。我对 ARC 还很陌生。 UITextView
的 delegate 属性定义为 assign
(ARC 不应该将其解释为 weak
吗?)。我知道这种方法在记忆方面相当奇怪。然而,我想知道 ARC 是否可以处理这样的事情。显然不能。知道可能是什么问题以及如何解决它吗?
I am working on an iOS SDK 4 project with ARC enabled.
My class MyTextView
(derived from UITextView with UITextViewDelegate
protocol) implements the following static method:
+ (void)showInViewController:(UIViewController*)viewController
{
MyTextView *textEdit = [[MyTextView alloc] init];
textEdit.delegate = textEdit;
[viewController.view addSubview:textEdit];
// Show the keyboard
[textEdit becomeFirstResponder];
}
In one of my view controllers I call the following:
[MyTextView showInViewController:self]
This crashes with warning: Unable to restore previously selected frame.
on becomeFirstResponder
. Looks like some stack related crash because of some cycle. I am fairly new to ARC. The delegate property of UITextView
is defined as assign
(shouldn't ARC interpret that as weak
?). I know this approach is rather strange memory-wise. However, I wanted to know if ARC can handle things like that. Obviously it can't. Any idea what might be the problem and how to solve it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不认为它与 ARC 和内存管理有任何关系,而只是一个更基本的问题,即
UITextView
不能成为其自身的委托。它被锁定在一个循环中。将日志消息放入textViewDidChangeSelection
中,您将看到它被重复调用。我认为这不是内存问题,而只是 UITextView 委托的逻辑问题。即使您不执行有问题的showInViewController
操作,而只是创建一个标准的UITextView
子类并尝试将其委托设置为自身,您也会看到同样奇怪的行为。I don't think it has anything to do with the ARC and memory management, but just a more fundamental problem that a
UITextView
cannot be a delegate of itself. It gets locked in a loop. Put a logging message intextViewDidChangeSelection
and you'll see it gets repeatedly invoked. Not a memory issue, methinks, but rather just a logic issue withUITextView
delegates. Even if you don't do your problematicshowInViewController
but just create a standardUITextView
subclass and try to set its delegate to itself, you'll see the same curious behavior.老帖子,但这是答案:
http://www.cocoabuilder.com/archive/cocoa/282093-uitextview-as-its-own-delegate-infinite-loop-on-keyboard-select.html
或此处
self.delegate = self;这样做有什么问题吗?
old post, but here is the answer:
http://www.cocoabuilder.com/archive/cocoa/282093-uitextview-as-its-own-delegate-infinite-loop-on-keyboard-select.html
or here aswell
self.delegate = self; what's wrong in doing that?