I found the answer. The solution is ti also use addChildViewConroller, not only addSubview. Hope it will help to someone...
UITextFieldDelegate crash (exc_bad_access)
-
25-06-2023 - |
문제
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:
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:
Any help please.
해결책 2
다른 팁
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.
제휴하지 않습니다 StackOverflow