Take the 2-minute tour ×

I have a PromoCodeViewController which is triggered using the following code:

@implementation Demo_WebServiceCallingUsingiOSAppDelegate

@synthesize window = _window,promoCodeController;


    self.promoCodeController = [[PromoCodeViewController alloc] init]; 

    [self.window addSubview:self.promoCodeController.view]; 

    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    return YES;

The PromoCodeViewController contains the UITextField and I implement the UITextFieldDelegate for the PromoCodeViewController as shown:

@interface PromoCodeViewController : UIViewController<UITextFieldDelegate>

    IBOutlet UITextField *promoCodeTextField; 

@property (nonatomic,retain) IBOutlet UITextField *promoCodeTextField; 


I implement the textFieldShouldReturn method as shown:

- (BOOL)textFieldShouldReturn:(UITextField *)textField
    return TRUE; 

I have even setup the PromoCodeViewController to be the delegate for the UITextField events. When I start typing in the TextBox it throws "Program received signal. EXC_BAD_ACCESS". It happens when I type second character in the UITextField. What am I doing wrong?


The error comes on the following section:

int main(int argc, char *argv[])
    @autoreleasepool {
        return UIApplicationMain(argc, argv, nil, NSStringFromClass([Demo_WebServiceCallingUsingiOSAppDelegate class]));
share|improve this question
I don't think you should be setting your window to you promoCodeViewController in the first code block... Though I don't think that is what is causing your error. –  Karoly S Aug 10 '11 at 15:45
what is the stack trace at the moment of the crash? –  sergio Aug 10 '11 at 15:46
@sergio There is nothing in the stacktrace! –  azamsharp Aug 10 '11 at 15:47
See my answer here. I had the same problem once and it seems to be related to a bug in the simulator SDK. –  omz Aug 10 '11 at 15:54
The EXC_BAD_ACCESS errors as far as I know are caused by trying to access something that no longer exists, IE wasn't retained enough, or was released early. It doesn't look like the code you posted has anything like that wrong, so it must be elsewhere. Are you detecting text being typed anywhere in your code? Try placing some breakpoints, or NSLog messages for yourself, and post the results. –  Karoly S Aug 10 '11 at 15:55
show 4 more comments

2 Answers

It looks as though you're missing an @synthesize statement for the promoCodeTextField property. Add the following to your Demo_WebServiceCallingUsingiOSAppDelegate class (which by the way, might benefit from renaming):

@synthesize promoCodeTextField = _promoCodeTextField;

Without the @synthesize statement, Xcode's Nib File Editor will directly set the instance variable instead of calling the accessor methods (since they don't exist); as a consequence the text field is never retained.

share|improve this answer
I have synthesize! It is a BUG in the simulator! –  azamsharp Aug 10 '11 at 16:02
There's no @synthesize statement for promoCodeTextField in the code you posted. –  jlehr Aug 10 '11 at 17:07
It's true that the promoCodeTextField isn't synthesized. That can't be the problem here however. First, the outlet is declared directly on the instance variable, so the nib-loading mechanism will use setValue:forKey: which can fall back to accessing the variable directly if there's no setter. In this case, the documentation states that the value will be retained. Even if that was not the case, the containing view would retain the text field. –  omz Aug 10 '11 at 17:30
add comment

What object is the delegate of the textfield? Objects do not usually retain their delegates (since the delegate is often retaining a reference to the object, and retain-cycles lead to memory leaks).

My hunch is that your text field's delegate is defined and attached in your nib, but isn't being retained anywhere, so shortly after the nib is loaded the delegate is deallocated. As soon as the text field attempts to do anything with the delegate, it crashes.

If so, make sure something (usually the application delegate) is retaining a reference to whatever object is serving as the delegate of your text field.

share|improve this answer
add comment

Your Answer


