2016-06-20 58 views

回答

1

使用以下代碼可以獲取搜索文本字段。把它寫在viewDidLoad

UITextField *searchBarTextField = [self.searchController.searchBar valueForKey:@"_searchField"]; 
[searchBarTextField setInputView:datePicker]; 

希望這將幫助你

+0

根本沒有Datepicker將顯示 –

+1

@property(強,非原子)UISearchController * searchController; –

+0

您需要在編程創建搜索欄後設置此行 –

0

第一組的代表,那麼你可以用下面的委託方法:

- (BOOL)searchBarShouldBeginEditing:(UISearchBar*)searchBar { 
    [self showDatePicker]; 
    return NO; 
} 

-(void) showDatePicker { 
    UIDatePicker *datePicker = [[UIDatePicker alloc]init]; 
    datePicker.datePickerMode = UIDatePickerModeDate; 
    [datePicker setDate:[NSDate date]]; 
    [datePicker addTarget:self action:@selector(showSelectedDate:) forControlEvents:UIControlEventValueChanged]; 
} 

- (void)showSelectedDate:(id)sender { 
    UIDatePicker *picker = (UIDatePicker*)self.date.inputView; 
    searchBar.text = [self formatDate:picker.date]; 
} 

你可以使用任何日期格式來顯示所需格式日期。

+0

- (BOOL)searchBarShouldBeginEditing:(*的UISearchBar)搜索欄{ [自showDatePicker] return NO;如果NO nothink將顯示。如果YES日期選擇器和鍵盤都會顯示 } –

+0

選項1:在searchBarShouldBeginEditing和'showDatePicker'中寫入[searchBar resignFirstResponder]; 選項2:在UISearchBar上添加點擊手勢,併爲其寫入@selector。在該手勢方法中,調用'showDatePicker'方法。 在這種情況下,請不要爲UISearchBar設置委託。我認爲這會起作用。 – Pushkraj

相關問題