Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I agree with @Inaziger analysis. The delegate of UITextField instance is a kind of weak reference. It does not hold the delegate assigned to it. According ARC, the delegate will be nil is no one hold a reference to it. Therefore, it will be up to assigner to keep it so the delegate will be called. You code prior workaround is something like this:</p> <pre><code>- (void) somemethod { ... id&lt;UITextFieldDelegate&gt; tempDelegate = [NumericTextFieldDelegate new]; textFieldName.delegate = tempDelegate; ... } </code></pre> <p>the instance of textFieldName got a reference to a delegate created locally in somethod. ARC will set temDelegate to nil after the method call. However, the text field's delegate still holds the a pointer to memory assigned to, which is released by ARC afterwards. That's why you got the bad memory access crash.</p> <p>By keeping the del as static var in your class, it will be kept during your app run cycle as long as you have not set it to nil. I think it is better to keep the static del as a class level member and to provide a setter so that you should remember to release it. Something like:</p> <pre><code>// in interface definition +(NumericTextFieldDelegate *) getDelegate; +(void) setDelegate:(id)newDel; // in implementation static NumericTextFieldDelegate* del; +(NumericTextFieldDelegate *) getDelegate { @synchronized(del) { if(del == nil) del = [NumericTextFieldDelegate new]; } return del; } +(void) setDelegate:(id)newDel { del = newDel; } </code></pre> <p>By the way, you can also keep your prior workaround codes as they are. You can keep the delegate in the class of text field as a class member variable or property.</p> <pre><code>@interface myTextFieldContainer () { @proerpty (strong) id&lt;UITextFieldDelegate&gt; delHolder; ... } @implementaion myTextFieldContainer { @sythysis delHolder = _delHodler; ... self.delHolder = [NumericTextFieldDelegate new]; textFieldName.delegate = self.delHolder; </code></pre> <p>The benefit of above strategy is that you would not worry about release the delegate when your view controller is gone.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload