Ios 编辑完文本字段后如何关闭键盘

Ios 编辑完文本字段后如何关闭键盘,ios,objective-c,Ios,Objective C,我有一个简单的程序,您可以在文本字段中键入文本,点击ok按钮,然后标签会随着输入的文本更新 当我按下OK按钮、按下背景中覆盖整个视图的大按钮或按下键盘上的return按钮时,我希望iPhone键盘消失。我一直在尝试使用 [textField resignFirstResponder] 方法,但它不起作用。程序编译得很好,但是当从这些事件中的任何一个调用此方法时,它会停止,我会收到一条消息说: 线程1:信号SIGABRT“ 我做错了什么 #import "ViewController.h

我有一个简单的程序,您可以在文本字段中键入文本,点击ok按钮,然后标签会随着输入的文本更新

当我按下OK按钮、按下背景中覆盖整个视图的大按钮或按下键盘上的return按钮时,我希望iPhone键盘消失。我一直在尝试使用

[textField resignFirstResponder]
方法,但它不起作用。程序编译得很好,但是当从这些事件中的任何一个调用此方法时,它会停止,我会收到一条消息说:

线程1:信号SIGABRT“

我做错了什么

    #import "ViewController.h"

    @interface ViewController ()



    @end

    @implementation ViewController

    @synthesize txtName;
    @synthesize lblMessage;

    - (void)viewDidLoad
    {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
    }

    - (void)didReceiveMemoryWarning
    {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }

    - (IBAction)doSomething:(UIButton *)sender
    {
        [txtName resignFirstResponder];

        NSString *msg = [[NSString alloc] initWithFormat:@"Hello, %@", txtName.text];
        [lblMessage setText:msg];

        //[msg release];
    }

    - (IBAction)makeKeyboardGoAway:(UIButton *)sender
    {
        [txtName resignFirstResponder];
    }

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

    @end
下面是头文件:

    #import <UIKit/UIKit.h>

    @interface ViewController : UIViewController

    @property (weak, nonatomic) IBOutlet UITextField *txtName;
    @property (weak, nonatomic) IBOutlet UILabel *lblMessage;

    - (IBAction)doSomething:(UIButton *)sender;
    - (IBAction)makeKeyboardGoAway:(UIButton *)sender;
    @end

这是可行的…如果所有的东西都符合这一点,那么可能是代码中的其他东西

…还要确保所有对象都链接正确(VC到TextField和Button…Button back到VC)…这也可能导致崩溃

h

m


单击OK按钮时调用此方法

[self.view endEditing:YES];

通常
信号SIGABRT
表示代码中的某个地方存在内存问题。在
txtName
中,您在
UITextField
object上的指针无效。很可能某些内容没有保留,或者在ARC上,您在某个地方指定了指针属性。将所有
txtName
替换为
self.txtName
和check.将txtName替换为self.txtName并没有修复错误。这与问题中发布的代码有什么关系?该代码已经在文本字段上调用了
resignFirstResponder
。这就是问题所在。它如何不相关?没有发布头文件…因此,加倍会是一个坏主意吗检查是否所有内容都是按照我发布的方式编写的?此代码用于将其分配给任何按钮以及要在返回键上释放的文本字段本身。如果您的代码与此匹配,则可能SIGABRT是从其他地方抛出的,您至少可以排除此情况。更新以包含发布代码的每个部分。可能他ad一个不同的方法链接到某个被错误删除的地方…可能还有其他东西…但这不可能引发错误。您能解释一下您发布的答案如何解决问题吗?您的代码如何解决SIGABRT运行时错误?您所做的只是发布一个名为
dis的随机方法小姐:
。另外,请解释一下你答案的第一句话。我不知道它是什么意思。我没有试过这个,但我找到了解决方法。
#import "ViewController.h"

@implementation ViewController

@synthesize txtName;
@synthesize lblMessage;

- (IBAction)doSomething
{
    [txtName resignFirstResponder];

    NSString *msg = [[NSString alloc] initWithFormat:@"Hello, %@",
                     txtName.text];
    [lblMessage setText:msg];
    //[msg release];
}

- (IBAction) makeKeyboardGoAway
{
    [txtName resignFirstResponder];
}

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


- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
@interface ViewController : UIViewController {

   IBOutlet UIButton *button;
   IBOutlet UITextField *textfield;
   IBOutlet UILabel *label;
   NSString *string;
}

-(IBAction)dismiss:(id)sender;


@property (nonatomic, retain)UIButton *button;
@property (nonatomic, retain)UITextField *textfield;
@property (nonatomic, retain)UILabel *label;
@synthesize textfield, button, label;

-(IBAction)dismiss:(id)sender {
[textfield resignFirstResponder];
string = [NSString stringWithFormat:@"Hello %@", textfield.text];
[label setText:string];
}
[self.view endEditing:YES];