2011-02-02 6 views
2

私は,,,,iphoneで入力を終了したら、キーボードを解除する方法?

- (void)viewDidLoad { 
    [super viewDidLoad]; 


    restaurant_name = [[UILabel alloc] initWithFrame:CGRectMake(20, 83, 135, 21)]; 
    [restaurant_name setBackgroundColor:[UIColor clearColor]]; 
    [restaurant_name setText: @"Restaurant Name"]; 
    [self.view addSubview:restaurant_name]; 

    restaurant_name_textfield = [[UITextField alloc] initWithFrame:CGRectMake(160, 80, 150, 31)]; 
    [restaurant_name_textfield setBackgroundColor:[UIColor clearColor]]; 
    [restaurant_name_textfield setBorderStyle:UITextBorderStyleRoundedRect]; 
    [restaurant_name_textfield resignFirstResponder]; 
    [self.view addSubview:restaurant_name_textfield]; 

    restaurant_name_save = restaurant_name_textfield.text; 
    //NSLog(restaurant_name_save); 

    picker_delivery = [[UILabel alloc] initWithFrame:CGRectMake(115, 113, 110, 25)]; 
    [picker_delivery setBackgroundColor:[UIColor clearColor]]; 
    [picker_delivery setFont:[UIFont fontWithName:@"Arial" size:18]]; 
    [picker_delivery setText: @"Pick/Delivery"]; 

    [self.view addSubview:picker_delivery]; 

    amount = [[UILabel alloc] initWithFrame:CGRectMake(20, 153, 150, 20)]; 
    [amount setText: @"Amount"]; 
    [amount setBackgroundColor:[UIColor clearColor]]; 
    [self.view addSubview:amount]; 

    amount_textfield = [[UITextField alloc] initWithFrame:CGRectMake(160,150 , 150, 31)]; 
    [amount_textfield setBackgroundColor:[UIColor clearColor]]; 
    [amount_textfield setBorderStyle:UITextBorderStyleRoundedRect]; 
    [self.view addSubview:amount_textfield]; 

    ready_in = [[UILabel alloc] initWithFrame:CGRectMake(20, 188, 150, 20)]; 
    [ready_in setText:@"Ready in"]; 
    [ready_in setBackgroundColor:[UIColor clearColor]]; 
    [self.view addSubview:ready_in]; 

    ready_in_textfield = [[UITextField alloc] initWithFrame:CGRectMake(160, 185, 150, 31)]; 
    [ready_in_textfield setBackgroundColor:[UIColor clearColor]]; 
    [ready_in_textfield setBorderStyle:UITextBorderStyleRoundedRect]; 
    [self.view addSubview:ready_in_textfield]; 

    reminder = [[UILabel alloc] initWithFrame:CGRectMake(20, 230, 150, 20)]; 
    [reminder setText: @"Reminder"]; 
    [reminder setBackgroundColor:[UIColor clearColor]]; 
    [self.view addSubview:reminder]; 

    mySwitch = [[UISwitch alloc] initWithFrame:CGRectMake(220, 230, 50, 50)]; 
    [self.view addSubview:mySwitch]; 



    start_button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    [start_button setFrame:CGRectMake(100, 300, 100, 50)]; 
    [start_button setTitle:@"Start" forState:UIControlStateNormal]; 
    //[myButton setImage:myButtonImage forState:UIControlStateNormal]; 

    [start_button addTarget:self action:@selector(saveData) 
     forControlEvents:UIControlEventTouchUpInside]; 



     [self.view addSubview:start_button]; 


} 



-(BOOL) textFieldShouldReturn:(UITextField*) textField { 
    [textField resignFirstResponder]; 
    return YES; 
} 

これはRestaurantViewController.m のコーディングされているが、私はリターンキー、

任意のアイデアを押すと、何も起こりません???以下のように、簡単なコードを書いています

+0

ようこそ!あなたのコードをコードブロックに入れるために投稿を編集しました。将来、コードを質問に貼り付けるときは、「{}」ボタンを押してコードブロックに折り返したり、すべての行の左余白に4つのスペースを入れたりします。 –

答えて

1

あなたの.hファイルには、この

@interface Book : UIViewController<UITextFieldDelegate> { 

のUITextField * amount_textfieldのようでなければならないはずです。

}

と実装ファイルがなければならない:

- (void)loadView { 

amount_textfield = [[UITextField alloc] initWithFrame:CGRectMake(60, 120, 200, 40)]; 
amount_textfield.delegate=self; 
[ContentView addSubview:amount_textfield];} 

- (BOOL)textFieldShouldReturn:(UITextFieldの*)テキストフィールド{ [テキストフィールドresignFirstResponder]。 returnはい。 }

+0

ありがとうございます、今は正常に動作しています... –

+0

@Veer - そうではありません。それはそうですが、実際には、UITextFieldが割り当てられ、amount_textfieldに格納されています。これによりアプリケーションがクラッシュします。メモリ管理を無視してください。 –

+0

@Veer - deallocでテキストフィールドを解放する必要があります。 – Prabh

0

テキストフィールドの代理人をコントローラに接続しましたか?例:restaurant_name.delegate = self? Returnキーを押したときにキーボードを閉じる必要があるすべてのテキストフィールドに対して、これを行う必要があります。

+0

いいえ、あなたがここに書いたようなものは書いていませんでしたが、どこに書きますか? –

3

ビューコントローラが<UITextFieldDelegate>プロトコルを採用していることを確認してください。その後selfにごUITextFieldsのそれぞれの.delegateプロパティを設定します。

amount_textfield.delegate = self; 

また、あなたがのallocとinit release「EDになるように必要なすべてを。この場合、それはおそらくaddSubview:コールの直後に起こり、そのようなものがスーパービューに追加されます。

+0

私はここに書かれているように、を採用しています@interface RestaurantViewController:UIViewController

+0

どこにamount_textfield.delegate = selfを書きますか? –

+0

直前にそれをサブビューに追加すると問題ありません。次に、それをサブビューgo: '[amount_textfield release];'に追加した後で、さもなければ、そのオブジェクトは漏れるでしょう。 –

関連する問題