2011-02-02 38 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

歡迎來到SO!我編輯了你的帖子,把你的代碼放到代碼塊中。將來,當您將代碼粘貼到問題中時,您需要點擊「{}」按鈕以將其包裝在代碼塊中,或者在每行的左邊緣放置四個空格。 – 2011-02-02 13:08:48

回答

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

您是否已將文本字段的代表連接到控制器?例如,restaurant_name.delegate = self?您必須對所有需要在按回車鍵時都解除鍵盤的文本字段執行此操作。

+0

不,我沒有寫過你寫的東西,我應該在哪裏寫? – 2011-02-02 13:15:30

3

確保您的視圖控制器採用<UITextFieldDelegate>協議。然後每個UITextFields的.delegate屬性設置爲self

amount_textfield.delegate = self; 

而且,一切,你的alloc和init必須release「版。在這種情況下,這應該會在addSubview:調用之後立即發生,這會將每個這樣的事情添加到超級視圖。

+0

我採用了,就像這裏寫的@interface RestaurantViewController:UIViewController 2011-02-02 13:13:35

+0

我應該在哪裏寫amount_textfield.delegate = self? – 2011-02-02 13:14:59