UIPickerView完成按钮不起作用

时间:2014-12-01 18:48:39

标签: ios objective-c uipickerview uibarbuttonitem toolbar

带工具栏的Picker视图,上面创建了Done按钮。点击完成按钮后它无法正常工作。

选择器视图向上滚动。点击完成按钮。

-(void)createPicker:(id)sender{

    pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0,100,0,0)];
    [pickerView setDataSource: self];
    [pickerView setDelegate: self];
    pickerView.showsSelectionIndicator = YES;
    [pickerView setBackgroundColor:[UIColor whiteColor]];

    UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
    toolBar.barStyle = UIBarStyleBlackOpaque;
    UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneTouched:)];
    [toolBar setItems:[NSArray arrayWithObjects:[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil], doneButton, nil]];
    [pickerView addSubview:toolBar];
}

点击完成按钮关闭pickerView

-(void)doneTouched:(id)sender{
     [pickerview removeFromSuperview];
 }

我不知道我在这里做错了什么。任何人都可以建议我如何调用在uipickerview工具栏按钮上添加的完成按钮方法。

点击完成选择器视图向上滚动,而不是调用方法 doneTouched:

@All 在此先感谢。

4 个答案:

答案 0 :(得分:5)

我已经解决了不知道是否正确实施的问题,但它对我有用。 下面是带有完成按钮的选择器视图的代码

-(void)createPickerView{

    pickerToolBarView = [[UIView alloc]initWithFrame:CGRectMake(0,self.view.frame.size.height/2, self.view.frame.size.width,400)];
    [pickerToolBarView setBackgroundColor:[UIColor whiteColor]];

    UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0,0,pickerToolBarView.frame.size.width,42)];
    toolBar.barStyle = UIBarStyleBlackOpaque;
    UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneTouched:)];
    [toolBar setItems:[NSArray arrayWithObjects:[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil], doneButton, nil]];

    pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0,toolBar.frame.size.height,toolBar.frame.size.width,100)];
    [pickerView setDataSource: self];
    [pickerView setDelegate: self];
    pickerView.showsSelectionIndicator = YES;
    [pickerView setBackgroundColor:[UIColor whiteColor]];

    [pickerToolBarView addSubview:toolBar];
    [pickerToolBarView addSubview:pickerView];
    [self.view addSubview:pickerToolBarView];
    [self.view bringSubviewToFront:pickerToolBarView];
    [pickerToolBarView setHidden:YES];   
}

/* Done Touched */
- (void)doneTouched:(UIBarButtonItem *)sender{
    // hide the  view
    NSLog(@"Done Touched");
    [pickerToolBarView setHidden:YES];
}

答案 1 :(得分:1)

而不是将工具栏添加为子视图:

multithreading

答案 2 :(得分:1)

我遇到了同样的问题。我的问题是我没有直接将选择器绑在文本字段上,我从一个按钮调出选择器。当我完成时,我还想要一个“完成”按钮来摆脱选择器。我认为问题在于我将工具栏作为选择器视图的一部分,由于某种原因我无法使按钮工作。我所做的是使工具栏成为视图的子视图,并将其y值设置为选择器的一角--44。这将它移动到选择器的顶部并允许按钮工作。 这里有很多关于这个问题的问题,但似乎没有一个问题适合我。我从Gabriel Theodoropoulos的例子开始:http://gabriel-tips.blogspot.com/2011/04/uipickerview-add-it-programmatically_04.html 并做了一些改变。这是我提出的,它似乎对我有用:

在.h文件中:

`   
    @interface Detail() <UIPickerViewDataSource, UIPickerViewDelegate>
    @property (nonatomic, retain) IBOutlet UIPickerView *picker;
    @property (nonatomic, retain) NSArray *pickerData;
    @property (assign, nonatomic) NSInteger pickerRowSelected;
    @property (nonatomic, retain) UIBarButtonItem *barButtonDone;
    @property (nonatomic, retain) UIToolbar *toolBar;
    @end

`

在.m文件中:`

-(void)showPicker{
    // Calculate the screen's width.
    float screenWidth = [UIScreen mainScreen].bounds.size.width;
    float pickerWidth = screenWidth * 3 / 4;

    // Calculate the starting x coordinate.
    float xPoint = screenWidth / 2 - pickerWidth / 2;

    // Init the picker view.
    picker = [[UIPickerView alloc] init];

    // Set the delegate and datasource. Don't expect picker view to work
    // correctly if you don't set it.
    [picker setDataSource: self];
    [picker setDelegate: self];

    // Set the picker's frame. We set the y coordinate to 50px.
    [picker setFrame: CGRectMake(xPoint, 150.0f, pickerWidth, 200.0f)];

    // Before we add the picker view to our view, let's do a couple more
    // things. First, let the selection indicator (that line inside the
    // picker view that highlights your selection) to be shown.
    picker.showsSelectionIndicator = YES;

    // Allow us to pre-select the first option in the pickerView.
    [picker selectRow:0 inComponent:0 animated:YES];

    [picker setBackgroundColor:[UIColor grayColor]];

    toolBar= [[UIToolbar alloc] initWithFrame:CGRectMake(0,150-44,pickerWidth,44)];

    barButtonDone = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStylePlain target:self action:@selector(itemWasSelected:)];
    [toolBar setItems:[NSArray arrayWithObjects: barButtonDone, nil]];

    // OK, we are ready. Add the picker in our view.
    [self.view addSubview: picker];

    // Add the done button to the picker view
    [self.view addSubview:toolBar];
    [toolBar becomeFirstResponder];
}


// The number of columns of data
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}

// The number of rows of data
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return pickerData.count;
}

// The data to return for the row and component (column) that's being passed in
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    return pickerData[row];
}

// Catpure the picker view selection
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    // This method is triggered whenever the user makes a change to the picker selection.
    // The parameter named row and component represents what was selected.
    self.pickerRowSelected  = row;
}

-(IBAction)itemWasSelected:(id)sender{

    NSLog(@"Picker selected = %@",[pickerData objectAtIndex: self.pickerRowSelected]);
    //NSLog(@"ID = %ld",(long)[self.thisData getIDForEntry:@"company" column:@"name" entry:[pickerData objectAtIndex: self.pickerRowSelected]]);
    [picker setHidden:YES];
    [toolBar setHidden:YES];

}


- (NSAttributedString *)pickerView:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component
{

    NSAttributedString *attString =
    [[NSAttributedString alloc] initWithString:[pickerData objectAtIndex:row] attributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];

    return attString;
}

//bring up the size Picker
//This is tied to a button from IB and when pressed will bring up
//the picker.  The index returned is stored in self.pickerRowSelected 
- (IBAction)sizeButtonPressed:(id)sender {

    self.pickerData = [self.thisData getSizePickerData];
    [self showPicker];

}

` 我相信有更好的方法可以实现这一点,但这对我有用,我现在正在使用它。

答案 3 :(得分:1)

我遇到了同样的问题。 问题是我将UIToolbar作为子视图添加到UIPickerView。

这就是我要解决的问题。

class MyViewController: UIViewController {
    var myPicker: UIPickerView!
    var myPickerToolBar: UIToolbar!

    func showPicker() {
         myPicker = UIPickerView()
         /*
         setup UIPickerView
         */
         view.addSubview(myPicker)

         myPickerToolBar = UIToolbar(frame: CGRect())
         /*
         setup UIToolbar with barButtons, one of barButtons runs hidePicker()
         */
         view.addSubview(myPickerToolBar)
    }

    @objc func hidePicker() {
        myPicker.removeFromSuperview()
        myPickerToolBar.removeFromSuperview()
    }
}