简体   繁体   English

UITextFieldDelegate崩溃(exc_bad_access)

[英]UITextFieldDelegate crash (exc_bad_access)

Looks like I have a problem with UITextFieldDelegate . 看起来我有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. 我刚刚创建了一个响应UITextFieldDelegate协议的视图控制器,并轻松地将字段添加到xib,然后设置委托字段......你知道。

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 。H

#import <UIKit/UIKit.h>

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

.m .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. 代表由IB设定。

Error screenshot: 错误截图:

在此输入图像描述

Any help please. 请帮忙。

Ensure you have this in your .h 确保你的.h中有这个

@interface TopBar : UIViewController <UITextFieldDelegate> {

}

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

and remove from the @interface 并从@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. 听起来你的field_top正在发布,而你正试图在以后访问它,这就是它崩溃的原因。

I found the answer. 我找到了答案。 The solution is ti also use addChildViewConroller, not only addSubview. 解决方案是使用addChildViewConroller,而不仅仅是addSubview。 Hope it will help to someone... 希望它会对某人有所帮助......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM