我如何告诉我的 UITextField(代码)放弃第一响应者?

发布于 2024-12-05 20:37:12 字数 1069 浏览 0 评论 0原文

我已经通过代码创建了 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

路还长,别太狂 2024-12-12 20:37:12

代码如何知道我正在引用 UITextField 而不是其他 UITextField

您的 textFieldShouldReturn: 方法的 textField 参数将始终是当前活动的文本字段。

该方法必须返回一个 BOOL,您应该会收到编译器警告。请

-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

注意,您当前添加文本字段的方式也在泄漏内存。您应该按照 WrightCS 的答案将其设置为属性,以便稍后参考。因此,在 viewDidLoad 的末尾:

self.myTextField = field;
[field release];

How does the code know that I am referending to an UITextField and no to other one

Your textFieldShouldReturn: method's textField 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. Try

-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

Note 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:

self.myTextField = field;
[field release];
指尖上的星空 2024-12-12 20:37:12

在标题中定义文本字段,然后您可以使用以下内容:

.h

@interface MyeViewController : UIViewController <UITextFieldDelegate>
{
    UITextField * myTextField;
}
@end

.m

-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [myTextField resignFirstResponder];

    /* textField here is referenced from 
       textFieldShouldReturn:(UITextField *)textField 
     */
}

Define your textField in your header, then you can use the following:

.h

@interface MyeViewController : UIViewController <UITextFieldDelegate>
{
    UITextField * myTextField;
}
@end

.m

-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [myTextField resignFirstResponder];

    /* textField here is referenced from 
       textFieldShouldReturn:(UITextField *)textField 
     */
}
惯饮孤独 2024-12-12 20:37:12

确保将以编程方式创建的 UITextFielddelegate 设置为 self(创建该对象的视图控制器)并实现适当的 UITextFieldDelegate 方法(我认为它的 textFieldShouldReturn:)并在传递给委托方法的 textField 参数上调用该方法 resignFirstResponder (这将是您的UI 文本字段)。

Make sure you set the delegate of your programatically created UITextField to self (the view controller that created the object) and implement the appropriate UITextFieldDelegate method (I think its textFieldShouldReturn:) and call in that method resignFirstResponder on the textField argument passed to the delegate method (which will be your UITextField).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文