我有一个touchesEnded事件,用于检查何时按下UITextField。我想要它做的是隐藏/显示UIPickerView。怎么办呢?
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [[event allTouches] anyObject];
if (CGRectContainsPoint([self.textField frame], [touch locationInView:self.view]))
{
NSString * error = @"Touched the TextField";
UIAlertView * errorAlert = [[UIAlertView alloc] initWithTitle:@"Selection!" message:error delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[errorAlert show];
//Want to show or hide UIPickerView
}
}
触摸发生时我已经分配了UIPickerView
@interface ThirdViewController : UIViewController <UITextFieldDelegate,UIPickerViewDelegate> {
IBOutlet UIPickerView *pickerView;
}
答案 0 :(得分:35)
切换“隐藏”属性将起到作用,但也会给出一个非常突然的显示。
避免这种情况的一种方法是将拾取器嵌入UIActionSheet中,从屏幕底部向上滑动。
以下是一个例子:
UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:nil
delegate:nil cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
CGRect pickerFrame = CGRectMake(0, 0, 0, 0);
UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame:pickerFrame];
pickerView.showsSelectionIndicator = YES;
pickerView.dataSource = self;
pickerView.delegate = self;
[sheet addSubview:pickerView];
[pickerView release];
[sheet showInView:view];
[sheet setBounds:CGRectMake(0, 0, 320, 415)];
self.actionSheet = sheet; // assuming you have setup a property to hold the action sheet
[sheet release];
当您使用了拣货员时,请将其解雇:
[self.actionSheet dismissWithClickedButtonIndex:0 animated:YES];
这种方法也可用于在选择器上方的栏中合并按钮(“完成”,“上一页”,“下一页”等) - 这是一个很好的解释如何做到here。
答案 1 :(得分:23)
因为我看到关于这些解决方案的评论不适用于iOS 7,我将假设此线程仍然相关并正在搜索。
我发现这样做的最好方法是将UIPickerView附加到(隐藏的)UITextField作为输入视图,如:
_myPicker = [[UIPickerView alloc] init];
_myPicker.delegate = self;
_myPicker.showsSelectionIndicator = YES;
myTextField.inputView = _myPicker;
如果需要,您始终可以隐藏文本字段。然后,您可以通过激活文本字段作为第一响应者来显示/隐藏UIPickerView,如:
[myTextField becomeFirstResponder];
[myTextField resignFirstResponder];
我已经在iOS 7上验证了这一点,并且我已经让它在iOS 5上工作了。
答案 2 :(得分:14)
UIPickerView继承自UIView,因此您应该只能切换其“隐藏”属性:
if (pickerView) pickerView.hidden = !pickerView.hidden;
答案 3 :(得分:5)
我正在使用虚拟UITextField
来显示/隐藏UIPickerView
首先,在@property
上添加一个UITextField作为UIViewController
。或者,添加UIPickerView
,以及
@property (strong, nonatomic) UITextField *dummyTextField;
其次,为UIPickerView
分配inputView
作为UITextField
。将自己分配为dataSource
和delegate
UIPickerView
。需要将UITextField
添加为UIViewController
view
的子视图。
- (void)viewDidLoad {
[super viewDidLoad];
UIPickerView *picker = [[UIPickerView alloc] init];
[picker setDataSource:self];
[picker setDelegate:self];
self.dummyTextField = [UITextField new];
self.dummyTextField.inputView = picker;
[self.dummyTextField setHidden:YES];
[self.view addSubview:self.dummyTextField];
}
最后,添加一种机制来显示和隐藏UIPickerView
。由于我使用的是假UITextField
,因此我决定使用以下UIBarButtonItem
添加名为“过滤器”的IBAction
:
- (IBAction)tappedFilterButton:(UIBarButtonItem *)sender {
if (self.dummyTextField.isFirstResponder) {
[self.dummyTextField resignFirstResponder];
}
else {
[self.dummyTextField becomeFirstResponder];
}
}
答案 4 :(得分:3)
答案 5 :(得分:2)
所以我使用了许多参考资料来试图解决这个问题,然后我最好的参考资料(信不信由你),来自UIView类参考文献中的苹果文档。
我在我的主视图顶部构建了一个迷你视图(“_pickerView”,其中包含UIToolBar,UIToolButton(带有IBAction“closePicker”)和UIPicker。
***请注意,这仅适用于iOS 4.0及更高版本
关闭和显示UIView(“_pickerView”)的代码是:
-(IBAction)closePicker:(id)sender
{
[UIView animateWithDuration:0.3 animations:^{
_pickerView.frame = CGRectMake(_pickerView.frame.origin.x,
460, //Displays the view off the screen
_pickerView.frame.size.width,
_pickerView.frame.size.height);
}];
}
-(IBAction)showPicker:(id)sender
{
[UIView animateWithDuration:0.3 animations:^{
_pickerView.frame = CGRectMake(_pickerView.frame.origin.x,
107, //Displays the view a little past the
//center ling of the screen
_pickerView.frame.size.width,
_pickerView.frame.size.height);
}];
}
并且对于你的viewDidLoad只需调用“closePicker”即可在加载时隐藏视图
答案 6 :(得分:1)
在基于scrollView的应用程序中,显示和隐藏UIPickerView可能是一个棘手的问题,因为将它固定到可见屏幕矩形的底部是相对困难的。您可以使用以下代码轻松完成。
let alertController = UIAlertController(title: title, message: "\n\n\n\n\n\n\n\n\n\n", preferredStyle: .ActionSheet)
alertController.view.addSubview(pickerView)
alertController.addAction(UIAlertAction(title: "OK", style: .Cancel) { action in
})
self.presentViewController(alertController, animated: true, completion: nil)
总而言之,您创建一个带有空消息的UIAlertController。为了使alertController具有pickerView的大小,您可以为消息提供必要数量的换行符。最后,您将pickerView添加为alertController.view
的子视图然后使用UIPickerViewDelegate方法跟踪选定的pickerView行。
答案 7 :(得分:0)
我到处寻找一个干净的方式来隐藏和显示(切换)UIPickerView使用一个按钮项目,只找到点点滴滴。对于那些希望这样做的人来说,这是我通过基本条件陈述的工作结果。
<强> ViewController.m 强>
- (IBAction)animatePicker {
if ([self.userSelection.title isEqualToString: (NSString *)@"Select"]) {
_userPicker.hidden = NO;
UIPickerView *pickerView = [[UIPickerView alloc] init]; // default frame is set
float pvHeight = pickerView.frame.size.height;
float y = [[UIScreen mainScreen] bounds].size.height - (pvHeight); // the root view of view controller
[UIView animateWithDuration:0.25f delay:0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
self.userPicker.frame = CGRectMake(0 , y, pickerView.frame.size.width, pvHeight);
} completion:nil];
} else if ([self.userSelection.title isEqualToString: (NSString *)@"Done"]) {
UIPickerView *pickerView = [[UIPickerView alloc] init]; // default frame is set
float pvHeight = pickerView.frame.size.height;
float y = [[UIScreen mainScreen] bounds].size.height - (pvHeight * -2); // the root view of view controller
[UIView animateWithDuration:0.25f delay:0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
self.userPicker.frame = CGRectMake(0 , y, pickerView.frame.size.width, pvHeight);
} completion:nil];
self.userSelection.title = @"Select";
}
}
所以这里发生了什么:我有一个名为“userSelection”的按钮项,标题为“Select”,隐藏的UIPickerView名为“userPicker”(要隐藏,只需复制一下“_userPicker.hidden”) ,将其粘贴到选择器声明中,并将布尔值设置为YES)。按钮项目连接到上述操作。在加载时(即当按钮的标题显示“选择”时),它取消隐藏选择器并将其设置为视图。您可以使用animateWithDuration和delay选项来控制该功能,但这些设置对我来说似乎很自然。
然后我有了这个方法,当选择了某些内容时,将按钮的标题更改为“完成”。我很肯定有一个更简洁的方法来做这一点,但切换方法给了我一些自由,以防我想稍后进行UI更改。
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
NSLog(@"Selected Row %ld", (long)row);
switch(row)
{
case 0:
self.userSelection.title = @"Done";
break;
case 1:
self.userSelection.title = @"Done";
break;
case 2:
self.userSelection.title = @"Done";
break;
case 3:
self.userSelection.title = @"Done";
break;
case 4:
self.userSelection.title = @"Done";
break;
case 5:
self.userSelection.title = @"Done";
break;
}
}
最后,操作以“else if”结束,当按钮显示“Done”时,用反向动画隐藏选择器(相同的代码,但带有“pvHeight * -2”),然后设置按钮的标题返回“选择”,用于完成整个动作的循环。
对于那里的专业人士来说可能是一种更简单的方式,但对于像我这样对这些东西不熟悉的人来说,这是最符合逻辑的。加上它有效,所以这总是一个奖励!
答案 8 :(得分:0)
以下是隐藏和显示带动画的UIPickerView
的示例代码: -
//显示
-(void)showPickerView{
self.pickerSheet = [UIAlertController alertControllerWithTitle:@"Select font" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
self.pickerView = [[UIPickerView alloc]initWithFrame:CGRectZero];
self.pickerView.dataSource = self;
self.pickerView.delegate = self;
self.pickerView.showsSelectionIndicator = YES;
[self.pickerView selectRow:1 inComponent:0 animated:YES];
[self.pickerSheet.view addSubview:self.pickerView];
self.pickerView.translatesAutoresizingMaskIntoConstraints = NO;
UIView *view = self.pickerView;
[self.pickerSheet.view addConstraints:[NSLayoutConstraint
constraintsWithVisualFormat:@"V:|[view]|"
options:0l
metrics:nil
views:NSDictionaryOfVariableBindings(view)]];
[self.pickerSheet.view addConstraints:[NSLayoutConstraint
constraintsWithVisualFormat:@"H:|[view]|"
options:0l
metrics:nil
views:NSDictionaryOfVariableBindings(view)]];
[self presentViewController:self.pickerSheet animated:YES completion:^{
}];
}
//隐藏
-(void)hidePickerView{
[self.pickerSheet dismissViewControllerAnimated:YES completion:^{
}];
}