我正在以编程方式使用UISearchController
,在点按Searchbar
textField
时,我希望将日期选择器设置为inputView
@property (strong, nonatomic) UISearchController *searchController;
文字字段的引用:iPhone display date picker on UITextField touch
但无法访问搜索栏Textfield。请帮忙。
答案 0 :(得分:1)
您可以使用以下代码获取搜索textField。将其写在viewDidLoad
UITextField *searchBarTextField = [self.searchController.searchBar valueForKey:@"_searchField"];
[searchBarTextField setInputView:datePicker];
希望这会对你有所帮助
答案 1 :(得分:0)
首先设置UISearchBar
的委托,然后您可以使用以下委托方法:
- (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];
}
您可以使用任何日期格式化程序以所需格式显示日期。