Search code examples
iosstaticuitextviewautomatic-ref-countinguitextviewdelegate

"self.delegate = self" not working on iOS using 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?


Solution

  • 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?