Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8036501
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T02:38:30+00:00 2026-06-05T02:38:30+00:00

I have created a delegate object implementing the UITextFieldDelegate in its own class named

  • 0

I have created a delegate object implementing the UITextFieldDelegate in its own class named NumericTextFieldDelegate then i have initialized the delegate in my controller in this way :

textFieldName.delegate = [NumericTextFieldDelegate new];

And i got this warning from the compiler :

Assigning retained object to unsafe property; object will be released after assignment

That means that the object will be released after the assignment and in fact when i run the application and i focus the UITextField i get an EXC_BAD_ACCESS and the app crash …

The only way to make it work that i found is creating a static variable with a factory method that dispatch the instance of the NumericTextFieldDelegate :

@interface NumericTextFieldDelegate : NSObject <UITextFieldDelegate>

+(NumericTextFieldDelegate *) getDelegate;

@end

@implementation NumericTextFieldDelegate

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {

    NSString *resultingString = [textField.text stringByReplacingCharactersInRange: range withString: string];

    // This allows backspace
    if ([resultingString length] == 0) {
        return true;
    }

    NSInteger holder;
    NSScanner *scan = [NSScanner scannerWithString: resultingString];

    return [scan scanInteger: &holder] && [scan isAtEnd];
}

+(NumericTextFieldDelegate *) getDelegate {
    static NumericTextFieldDelegate *del;
    @synchronized(del) {
        if(del == nil)
            del = [NumericTextFieldDelegate new];
    }
    return del;
}

@end

And then when i assign the delegate in this way :

textFieldName.delegate = [NumericTextFieldDelegate getDelegate];

everything works well, but my question is :

Why can’t i simply assign an anonymous new instance of the class ?
Why the object is automatically released after the assignment ?

Why do i need this workaround ?

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T02:38:32+00:00Added an answer on June 5, 2026 at 2:38 am

    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:

    - (void) somemethod {
    ...
    id<UITextFieldDelegate> tempDelegate = [NumericTextFieldDelegate new];
    textFieldName.delegate = tempDelegate;
    ...
    }
    

    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.

    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:

    // 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;
    }
    

    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.

    @interface myTextFieldContainer () {
    @proerpty (strong) id<UITextFieldDelegate> delHolder;
    ...
    }
    
    @implementaion myTextFieldContainer {
    @sythysis delHolder = _delHodler;
    ...
    self.delHolder = [NumericTextFieldDelegate new];
    textFieldName.delegate = self.delHolder;
    

    The benefit of above strategy is that you would not worry about release the delegate when your view controller is gone.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a weird issue. I have created a view controller with a nib
I have created a new Bitmap object and now want do draw some text
I have created objects that are interfaces to a web service. One typical object
My app has a singleton class called CycleManager. I have created a sealed class
I have a class MyCLController with a property dataSource that is data source delegate
I have an object pool, and I need to call a delegate method OnFree(),
I have an object declared in my app delegate: @interface MyAppDelegate : NSObject <UIApplicationDelegate>
in my C#-Silverlight-3-Application, I have created a class, that is doing some calculations that
I have created a QComboBox delegate which I use for a single column of
I created a protocol and assigned it to a delegate object @protocol AppBrainDelegate <NSObject>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.