带有完成按钮的 UIPickerView 不起作用
Posted
技术标签:
【中文标题】带有完成按钮的 UIPickerView 不起作用【英文标题】:UIPickerView with done button not working 【发布时间】:2014-12-01 18:48:38 【问题描述】:带有工具栏的选取器视图,在其上创建了完成按钮。单击完成按钮后它不起作用。
选择器视图向上滚动。点击完成按钮。
-(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:
@全部 提前致谢。
【问题讨论】:
您的选择器视图没有框架:CGRectMake(0,100,0,0) 我指出这一点是因为这很可能是您无法与工具栏交互的原因,因为从技术上讲,工具栏不在 UIPickerView 框架内。 @LyndseyScott 我无法弄清楚。我做错了什么。 尝试将 UIPickerView 设置为pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0,100,self.view.frame.size.width,44)];
之类的东西(与工具栏相同)
@LyndseyScott 这就是我所做的 pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0,self.view.frame.size.height-200,self.view.frame.size.width ,100)];对于工具栏 UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width,42)];
【参考方案1】:
我已经解决了不知道是否正确实施的问题,但它对我有用。 下面是带有完成按钮的选择器视图的代码
-(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];
【讨论】:
【参考方案2】:而不是将工具栏添加为子视图:
yourTextField.inputAccessoryView = toolBar;
【讨论】:
如果我没有文本字段怎么办?【参考方案3】:我遇到了同样的问题。我的问题是我没有将选择器直接绑定到文本字段,而是从按钮中调出选择器。当我完成时,我还想要一个“完成”按钮来摆脱选择器。我认为问题在于我将工具栏作为选择器视图的一部分,并且由于某种原因我无法让按钮工作。我所做的是使工具栏成为视图的子视图,并将其 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];
` 我确信有更好的方法来实现这一点,但这对我有用,我现在正在使用它。
【讨论】:
【参考方案4】:我遇到了同样的问题。 问题是我将 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()
【讨论】:
以上是关于带有完成按钮的 UIPickerView 不起作用的主要内容,如果未能解决你的问题,请参考以下文章
如何将带有完成按钮的工具栏添加到 UIPickerView?
Swift - UIPickerView 的带有完成按钮的 UIToolbar 在横向上消失