2013-02-21 97 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]; 
相關問題