2014-12-01 132 views
1

带工具栏的选取器视图,创建完成按钮。点击完成按钮不起作用。UIPickerView与完成按钮不起作用

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

-(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 在此先感谢。

+0

您的选择器视图没有框架:CGRectMake(0,100,0,0) – 2014-12-01 18:55:08

+0

我在指出这一点,因为它可能是您无法与工具栏进行交互的原因,因为工具栏并非技术上的在UIPickerView框架内。 – 2014-12-01 19:02:27

+0

@LyndseyScott我无法弄清楚。我做错了什么。 – KkMIW 2014-12-01 19:20:10

回答

4

我已经解决了不知道的问题是否正确实施的问题,但它对我有用。 下面完成按钮

-(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

而不是添加工具栏作为子视图的选择器视图代码做:

yourTextField.inputAccessoryView = toolBar; 
0

我有同样的问题。我的问题是我没有直接将选取器绑定到文本字段,而是从一个按钮调出选取器。我还想要一个“完成”按钮,在我完成时摆脱选择器。我想,问题在于我将工具栏作为选取器视图的一部分,出于某种原因,我无法使按钮工作。我所做的是使工具栏成为视图的子视图,并将其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]; 

} 

` 我确定有更好的方法来实现这一点,但这对我有用,我现在正在使用它。