テキスト フィールドにテキストを入力し、[OK] ボタンをクリックすると、入力したテキストでラベルが更新される簡単なプログラムがあります。
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