2012-09-21 128 views
1

因爲當我點擊的UITextField選取器顯示與鍵盤一起的日期,我想隱藏在DOB的文本字段中的鍵盤操作呢?下面我的代碼禁用鍵盤上的UITextField上編輯

- (void)removeViews:(id)object 
    { 
    [[self.view viewWithTag:9] removeFromSuperview]; 
    [[self.view viewWithTag:10] removeFromSuperview]; 
    [[self.view viewWithTag:11] removeFromSuperview]; 
    } 

    - (void)dismissDatePicker:(id)sender 
    { 
    CGRect toolbarTargetFrame = CGRectMake(0, self.view.bounds.size.height, 320, 44); 
    CGRect datePickerTargetFrame = CGRectMake(0, self.view.bounds.size.height+44, 320, 216); 
    [UIView beginAnimations:@"MoveOut" context:nil]; 
    [self.view viewWithTag:9].alpha = 0; 
    [self.view viewWithTag:10].frame = datePickerTargetFrame; 
    [self.view viewWithTag:11].frame = toolbarTargetFrame; 
    [UIView setAnimationDelegate:self]; 
    [UIView setAnimationDidStopSelector:@selector(removeViews:)]; 
    [UIView commitAnimations]; 
    } 

    - (IBAction)but 
    { 
    //[eventText resignFirstResponder]; 
    [dob resignFirstResponder]; 


    if ([self.view viewWithTag:9]) 
    { 
     return; 
    } 
    CGRect toolbarTargetFrame = CGRectMake(0, self.view.bounds.size.height-216-44, 320, 44); 
    CGRect datePickerTargetFrame = CGRectMake(0, self.view.bounds.size.height-216, 320, 216); 

    UIView *darkView = [[UIView alloc] initWithFrame:self.view.bounds ]; 
    darkView.alpha = 0; 
    darkView.backgroundColor = [UIColor blackColor]; 
    darkView.tag = 9; 
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissDatePicker:)] ; 
    [darkView addGestureRecognizer:tapGesture]; 
    [self.view addSubview:darkView]; 

    datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height+44, 320, 216)] ; 
    datePicker.datePickerMode=UIDatePickerModeDate; 
    datePicker.tag = 10; 
    [datePicker addTarget:self action:@selector(changeDate:) forControlEvents:UIControlEventValueChanged]; 

    [self.view addSubview:datePicker]; 

    UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height, 320, 44)] ; 
    toolBar.tag = 11; 
    toolBar.barStyle = UIBarStyleBlackTranslucent; 
    UIBarButtonItem *spacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil] ; 
    UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(dismissDatePicker:)] ; 
    [toolBar setItems:[NSArray arrayWithObjects:spacer, doneButton, nil]]; 
    [self.view addSubview:toolBar]; 

    [UIView beginAnimations:@"MoveIn" context:nil]; 
    toolBar.frame = toolbarTargetFrame; 
    datePicker.frame = datePickerTargetFrame; 
    darkView.alpha = 0.5; 
    [UIView commitAnimations]; 
    //[datePicker addTarget:self action:@selector(dateText:)forControlEvents:UIControlEventValueChanged]; 
    //NSDateFormatter *_dateFormatter = [[NSDateFormatter alloc] init]; 
    //_dateFormatter.dateStyle = NSDateFormatterFullStyle; 
    //dateText.text = [NSString stringWithFormat:@"%@", 
    // [_dateFormatter stringFromDate:datePicker.date]]; 

    //[self.tableview reloadData]; 

    } 

I added the resignfirstresponder also,since its showing the same error 

    - (BOOL)textFieldShouldReturn:(UITextField *)textField 
    { 
    return [textField resignFirstResponder]; 
    return [txt1 resignFirstResponder];//dob textfield 
    } 

enter image description here

+0

on textFielBeginEditing,只是辭職你的鍵盤和加載選取器 – Rajneesh071

回答

6

使用的UITextField的委託方法

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField 
{ 
    if(textField == dob-text) 
    //load picker here 
    return NO; //hide keyboard 
    else 
    return YES; //show keyboard 
} 
0

嘗試textFieldDidBeginEditing

-(void) textFieldDidBeginEditing:(UITextField *)textField{ 
    [textField resignFirstResponder]; 
} 
0
- (void)textFieldDidBeginEditing:(UITextField *)textField 
{ 
    datePicker.hidden = NO; 
    [textField resignFirstResponder]; 
} 
3

更好的解決方案是設置UIPickerView作爲inputViewUITextField

而且您可以將UIToolBar設置爲inputViewinputAccessoryView。 這樣iOS將處理所有UIPickView的顯示。

剛剛成立的屬性頂部的正確觀點:

self.dateTextField.inputView = self.datePicker; 
self.dateTextField.inputAccessoryView = self.inputToolbar; 

在另一方面,越來越觀點與viewWithTag:方法意味着,你是循環直通各方面的意見。爲什麼不爲這些視圖創建屬性。它會更少雜亂,可能會更快。

+0

可以用我的代碼解釋 – iosdev

+2

對不起,我沒有時間寫你的代碼。 – rckoenes

0

首先textfield不應該有一個popover - 既不picker也不popover也沒有任何東西 最好的解決方案是將一個按鈕類型的自定義設置圖像像日期選擇器圖像和添加選擇器 - 在這種情況下,你會沒有任何關於textfields popover的問題,外觀也不錯,你可以按照你的要求定製它。