Question

Looks like I have a problem with UITextFieldDelegate.

I just created a view controller that responds to UITextFieldDelegate protocol, and easily added the field to the xib, then set delegate field...you know.

But when I trying to press the field (to start editing, the program crashes).

Same thing happens when I trying to create field programmatically.

Here is call stack:

enter image description here

Here is full code:

.h

#import <UIKit/UIKit.h>

@interface TopBar : UIViewController <UITextFieldDelegate>
{
    IBOutlet UITextField * field_top;
}

.m

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    NSLog(@"textFieldShouldBeginEditing");
    textField.backgroundColor = [UIColor colorWithRed:220.0f/255.0f green:220.0f/255.0f blue:220.0f/255.0f alpha:1.0f];
    return YES;
}

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    NSLog(@"textFieldDidBeginEditing");
}

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
    NSLog(@"textFieldShouldEndEditing");
    textField.backgroundColor = [UIColor whiteColor];
    return YES;
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    NSLog(@"textFieldDidEndEditing");
}

Delegate is set by IB.

Error screenshot:

enter image description here

Any help please.

Was it helpful?

Solution 2

I found the answer. The solution is ti also use addChildViewConroller, not only addSubview. Hope it will help to someone...

OTHER TIPS

Ensure you have this in your .h

@interface TopBar : UIViewController <UITextFieldDelegate> {

}

@property (nonatomic, weak) IBOutlet UITextField *field_top;

and remove from the @interface

IBOutlet UITextField * field_top;

It sounds like your field_top is being released and you're trying to access it later, thats why it's crashing.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top