0

テキスト フィールドにテキストを入力し、[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
4

2 に答える 2

0

[OK]ボタンがクリックされたときにこのメソッドを呼び出します。

[self.view endEditing:YES];
于 2013-02-21T02:13:06.777 に答える
-1

これは機能します....すべてがこれと一致する場合、それはあなたのコード内の何か他のものである可能性があります....

...また、すべてのオブジェクトが正しくリンクされていることを確認してください(VCからTextFieldおよびButton .... 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];
}
于 2013-02-21T00:24:23.563 に答える