iOS很遗憾没有像标签那样的html下拉选择器。我决定我终于要为我的应用创建一个,它看起来很棒。我的下拉对象是UITextField的子类。但是,我改变了一些东西,现在它只在某些时候起作用。
已启用用户互动,但我不希望文本字段可编辑。我的dropdown子类所在的类是UITextField委托,应该接收UITextField的委托方法。
我有- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
我检查有问题的文本字段是否是下拉菜单,如果是,我调用一个方法来实例化一个弹出窗口并禁用编辑,但下拉列表只显示在其他所有轻按。
例如,我将点击“textfield”并显示我的popover。我点击,弹出消失,然后点击“文本字段”,没有任何反应。我再次点击文本字段,然后出现弹出窗口。不知道为什么会这样,这就是我正在做的事情:
·H
subclass : UIViewController<UITextFieldDelegate>
的.m
dropdownTextField.delegate = self;
...
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
if(textField == self.measurementSelect){
NSLog(@"IM CALLED");
[self showPopover:textField];
return NO;
}
return YES;
}
-(void)showPopover:(id)sender{
if (_measurementPicker == nil) {
_measurementPicker = [[iPadMeasurementSelect alloc] initWithStyle:UITableViewStylePlain];
_measurementPicker.delegate = self;
}
if (_measurementPopover == nil) {
_measurementPopover = [[UIPopoverController alloc] initWithContentViewController:_measurementPicker];
[_measurementPopover presentPopoverFromRect:self.measurementSelect.frame inView:self.conversionView permittedArrowDirections:UIPopoverArrowDirectionLeft animated:YES];
}
else {
[_measurementPopover dismissPopoverAnimated:YES];
_measurementPopover = nil;
}
}
每次点击都会被记录,所以我认为我的popover方法是这个问题的罪魁祸首。有什么想法吗?
答案 0 :(得分:2)
让我们通过戏弄UI元素的存在和popover的可见状态来重写:
// canonical lazy getters for UI elements
- (iPadMeasurementSelect *)measurementPicker {
if (!_measurementPicker) {
_measurementPicker = [[iPadMeasurementSelect alloc] initWithStyle:UITableViewStylePlain];
_measurementPicker.delegate = self;
}
return _measurementPicker;
}
- (UIPopoverController *)measurementPopover {
if (!_measurementPopover) {
_measurementPopover = [[UIPopoverController alloc] initWithContentViewController:self.measurementPicker];
}
return _measurementPopover;
}
// now the show/hide method makes sense. it can take a bool about whether to show or hide
-(void)showPopover:(BOOL)show {
if (show) {
[self.measurementPopover presentPopoverFromRect:self.measurementSelect.frame inView:self.conversionView permittedArrowDirections:UIPopoverArrowDirectionLeft animated:YES];
} else {
[self.measurementPopover dismissPopoverAnimated:NO];
// if you want/need to create a new one each time it is shown, nil the popover here, like this:
// self.measurementPopover = nil;
}
}
当textField开始编辑时,显示如下的弹出窗口:
[self showPopover:YES];
当委托获得didEndEditing消息时:
[self showPopover:NO];