我如何告诉我的 UITextField(代码)放弃第一响应者?
我已经通过代码创建了 UITextField,没有 InterfaceBuilder。我希望按下“完成”按钮时键盘消失。代码如何知道我正在引用 UITextField 而不是其他 UITextField
首先,非常感谢。
我的代码是这样的:
-(void)viewDidLoad {
[super viewDidLoad];
field = [[UITextField alloc] initWithFrame:CGRectMake(30, 100, 185, 30)];
field.adjustsFontSizeToFitWidth = YES;
field.textColor = [UIColor blackColor];
field.placeholder = @"Text";
field.keyboardType = UIKeyboardTypeDefault;
field.returnKeyType = UIReturnKeyDone;
field.backgroundColor = [UIColor blueColor];
field.delegate = self;
[self.view addSubview:field];
}
......
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
}
使用此代码,我按下“完成”按钮,但什么也没有发生。你说的是这样吗?
编辑:
我创建了两个 UITextField,就像处理前一个 UITextField 一样。但现在,对于每一行,我都这样做:
if ([indexPath row] == 0) {
[cell.contentView addSubview:pTextField];
}
else {
[cell.contentView addSubview:pTextField];
}
因此,使用这段代码,程序收到信号“EXC_BAD_ACCESS”。知道为什么会发生这种情况吗?
I have created my UITextField by code, without InterfaceBuilder. I want the keyboard to disappear when the button "Done" is pushed. How does the code know that I am referending to an UITextField and no to other one
First, thanks a lot.
My code is like this:
-(void)viewDidLoad {
[super viewDidLoad];
field = [[UITextField alloc] initWithFrame:CGRectMake(30, 100, 185, 30)];
field.adjustsFontSizeToFitWidth = YES;
field.textColor = [UIColor blackColor];
field.placeholder = @"Text";
field.keyboardType = UIKeyboardTypeDefault;
field.returnKeyType = UIReturnKeyDone;
field.backgroundColor = [UIColor blueColor];
field.delegate = self;
[self.view addSubview:field];
}
......
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
}
With this code I push the button Done and nothing happen. Is like that how you say?
Edit:
I've created two UITextField how I did with the previous one. But now, for each row I do this:
if ([indexPath row] == 0) {
[cell.contentView addSubview:pTextField];
}
else {
[cell.contentView addSubview:pTextField];
}
So with this code, the program received signal "EXC_BAD_ACCESS". Any idea why happen this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的
textFieldShouldReturn:
方法的textField
参数将始终是当前活动的文本字段。该方法必须返回一个
BOOL
,您应该会收到编译器警告。请注意,您当前添加文本字段的方式也在泄漏内存。您应该按照 WrightCS 的答案将其设置为属性,以便稍后参考。因此,在
viewDidLoad
的末尾:Your
textFieldShouldReturn:
method'stextField
parameter will always be the text field that is currently active.The method has to return a
BOOL
, you should be getting compiler warnings with it as it stands. TryNote that you are also currently leaking memory in the way you add the text field. You should set it as a property as per WrightCS's answer so that you can refer to it later on. So at the end of your
viewDidLoad
:在标题中定义文本字段,然后您可以使用以下内容:
.h
.m
Define your textField in your header, then you can use the following:
.h
.m
确保将以编程方式创建的
UITextField
的delegate
设置为self
(创建该对象的视图控制器)并实现适当的UITextFieldDelegate
方法(我认为它的textFieldShouldReturn:
)并在传递给委托方法的 textField 参数上调用该方法resignFirstResponder
(这将是您的UI 文本字段)。Make sure you set the
delegate
of your programatically createdUITextField
toself
(the view controller that created the object) and implement the appropriateUITextFieldDelegate
method (I think itstextFieldShouldReturn:
) and call in that methodresignFirstResponder
on the textField argument passed to the delegate method (which will be your UITextField).