如何以编程方式在按钮上隐藏UIPickerview?

时间:2015-12-28 10:53:52

标签: ios uibutton uipickerview

  

我在uitablview原型单元格中有一个uipickerivew,我想只在完成按钮按下时隐藏。请帮帮我。

这是我如何创建uipickeriview和完成按钮的代码。

 self.pickerView = [[UIPickerView alloc] initWithFrame:(CGRect){{0, 0}, 330, 200}];
self.pickerView.delegate = self;
self.pickerView.dataSource = self;
self.pickerView.center = (CGPoint){160, 640};
self.pickerView.hidden = YES;
self.pickerView.backgroundColor =[UIColor whiteColor];

[self.view addSubview:self.pickerView];





UIToolbar *toolBar= [[UIToolbar alloc] initWithFrame:CGRectMake(0,0,320,44)];
[toolBar setBarStyle:UIBarStyleBlackOpaque];
UIBarButtonItem *flex = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];

UIBarButtonItem *barButtonDone = [[UIBarButtonItem alloc] initWithTitle:@"Done"
                                                                  style:UIBarButtonItemStyleBordered
                                                                 target:self
                                                                 action:@selector(pickerDoneClicked)];
toolBar.items = @[barButtonDone];
toolBar.items = @[flex, barButtonDone];
barButtonDone.tintColor = [UIColor lightGrayColor];
[_pickerView addSubview:toolBar];


-(void) pickerDoneClicked {

[_pickerView resignFirstResponder];

}

5 个答案:

答案 0 :(得分:3)

最好不要每次都创建和删除,在视图中创建一个pickerview,然后在你的单元格中执行:

[_pickerView becomeFirstResponder];

(将其显示为动画键盘)。

然后,您可以创建一个pickerDoneClicked方法(在点击Done时调用)在您的选择器视图上调用resignFirstResponder(为其设置动画):

-(void) pickerDoneClicked {
[_pickerView resignFirstResponder];
}

答案 1 :(得分:0)

只需添加以下IBAction方法并调用removefromsuperview方法,

-(void)pickerDoneClicked{
[self.pickerView removeFromSuperview];}

由于您每次都要再次添加,因此可以直接从superview中删除它并再次添加。

答案 2 :(得分:0)

使用选择器视图选择值后,您可以实现此方法。添加带有完成按钮的工具栏并为完成按钮提供操作

- (IBAction)doneClicked:(id)sender {
 [yourTextfield resignFirstResponder];
}

在使用选择器作为输入选择文本字段的值时,使用此方法。

答案 3 :(得分:0)

- (void)viewDidLoad {

  self.pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 0, 330, 200)];
  self.pickerView.delegate = self;
  self.pickerView.dataSource = self;
  self.pickerView.backgroundColor =[UIColor whiteColor];

 [self.view addSubview:self.pickerView];

   UIToolbar *toolBar= [[UIToolbar alloc] initWithFrame:CGRectMake(0,0,320,44)];
  [toolBar setBarStyle:UIBarStyleBlackOpaque];
 UIBarButtonItem *barButtonDone = [[UIBarButtonItem alloc] initWithTitle:@"Done"
                                                                 style: UIBarButtonItemStylePlain
                                                                target:self
                                                                action:@selector(pickerDoneClicked:)];
   UIBarButtonItem *flex = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];

        toolBar.items = @[flex, barButtonDone];
       barButtonDone.tintColor = [UIColor lightGrayColor];
     [self.view addSubview:toolBar];


   }


-(IBAction)pickerDoneClicked:(id)sender
  {
         pickerView.hidden = YES;

   }

答案 4 :(得分:-1)

在didiSelectRow方法中添加以下代码行:

1 ::  0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1
2 ::  0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1
3 ::  0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 1
4 ::  0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0
5 ::  0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 1
6 ::  0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0
7 ::  0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1
8 ::  0 0 0 0 0 0 0 0 0 0 0 1 0 0 1 0
9 ::  0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0
10 ::  0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 1
11 ::  0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 1
12 ::  0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 0
相关问题