我有一個簡單的程序,您可以在文本字段中鍵入文本,點擊確定按鈕,並使用輸入的文本更新標籤。如何在完成編輯文本字段時關閉鍵盤
我想要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
頭文件plz – 2013-02-21 00:03:46
通常'signal SIGABRT'表示你的代碼中有內存問題。它看起來像在'txtName'中,'UITextField'對象上有無效指針 – Nekto 2013-02-21 00:08:08
很可能不會保留應該是的東西,或者,在ARC上,你會在某處指定一個指針屬性。 – 2013-02-21 00:33:19