我有一个简单的程序,您可以在文本字段中键入文本,点击确定按钮,并使用输入的文本更新标签。
当我按下OK按钮,按下背景中覆盖整个视图的大按钮,或者按下键盘上的返回按钮时,我希望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
嗯,我得到了它的工作,但我仍然不明白我得到的错误信息。 这是适用于我的代码。
部首:
#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
答案 0 :(得分:0)
单击“确定”按钮时调用此方法。
[self.view endEditing:YES];
答案 1 :(得分:-1)
可能不是resingFirstResponder
事......
试一试:
创建名为takeKeyboardAway
的方法:
-(void)takeKeyboardAway{[sender resingFirstResponder];}
然后随时调用它:
[self takeKeyboardAway];
答案 2 :(得分:-1)
这很有效....如果所有内容与此相符,那么它可能是您代码中的其他内容....
...还要确保所有对象都正确链接(VC到TextField和Button ....按钮回到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];
}