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