2013-02-21 83 views
0

我有一个简单的程序,您可以在文本字段中键入文本,点击确定按钮,并使用输入的文本更新标签。如何在完成编辑文本字段时关闭键盘

我想要iPhone键盘消失,当我按OK按钮时,当我按下覆盖整个视图的背景中的大按钮,或者当我按下键盘上的返回按钮时。我一直试图使用

[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 

那么我得到它的工作,但我仍然不明白我得到的错误信息。 这是我工作的代码。

页眉:

#import <UIKit/UIKit.h> 

@interface ViewController : UIViewController 
{ 
    IBOutlet UITextField *txtName; 
    IBOutlet UILabel *lblMessage; 
} 
@property (nonatomic, retain) IBOutlet UITextField *txtName; 
@property (nonatomic, retain) IBOutlet UILabel *lblMessage; 

- (IBAction)doSomething; 
- (IBAction)makeKeyboardGoAway; 

@end 

实现:

#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 
+2

头文件plz – 2013-02-21 00:03:46

+1

通常'signal SIGABRT'表示你的代码中有内存问题。它看起来像在'txtName'中,'UITextField'对象上有无效指针 – Nekto 2013-02-21 00:08:08

+0

很可能不会保留应该是的东西,或者,在ARC上,你会在某处指定一个指针属性。 – 2013-02-21 00:33:19

回答

-1

这可能不是resingFirstResponder事情...

尝试:

  1. 创建方法南ED takeKeyboardAway

    -(void)takeKeyboardAway{[sender resingFirstResponder];}

  2. ,然后调用它,只要你想要的:

    [self takeKeyboardAway];

+0

急救员... lalalalalaaaaaaa ..急救员... lalalaaaaa(抱歉,无法抗拒ResingFirstResponder笑话:-) – TheEye 2013-02-22 00:33:08

+0

@TheEye你需要去看医生! :D haha​​ha – 2013-02-22 23:52:04

-1

这个工程....如果一切行了这一点,可能是别的东西在你的代码....

...也一定要正确链接所有对象(VC到TextField和按钮....按钮返回到VC)...这也可能导致崩溃

.H

@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; 

.M

@synthesize textfield, button, label; 

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

这与这个问题中发布的代码有什么关系?该代码已经在文本字段中调用了'resignFirstResponder'。这就是问题所在。 – rmaddy 2013-02-21 01:09:12

+0

它与它没有关系?没有发布头文件....因此,仔细检查一下所有内容是否为我发布的内容会是一个坏主意?此代码适用于将其分配给任何按钮以及文本字段本身,以便在返回键上释放。如果你的代码与此匹配,那么SIGABRT可能会从其他地方抛出,你至少可以排除这一点。 – 2013-02-21 04:48:42

+0

更新为包含发布代码的每个部分。也许他有一个不同的方法链接到某个地方被错误删除....也许还有别的东西......但没有办法,这会抛出一个错误。 – 2013-02-21 05:02:57

0

调用此方法是单击OK按钮时。

[self.view endEditing:YES]; 
相关问题