复制粘贴解决方案!
这适用于 iOS 10(可能还有其他版本)我在 2017 年 9 月 4 日编写了这段代码。
我需要结合其他答案来获得我想要的,这是一个Cancel 按钮、Done 按钮,以及一个将在点击按钮时显示/隐藏的选择器视图。忽略我的屏幕截图在选择器中只有一项的事实。我的数组中只有一项。我已经测试了多个项目,效果很好。
免责声明!我已经测试并使用了这段代码。我在示例中概括了名称,因此如果我错过了一个并且它们的属性名称不对齐,我提前道歉。
为了让点击按钮时出现pickerView,您需要在视图控制器的任何位置创建一个dummyTextField(类型为UITextField。这是因为UIPickerView' s 旨在与 UITextFields 一起使用。为了模拟选择器的显示和隐藏,您的按钮点击需要调用 dummyTextField 的 becomeFirstResponder 和 resignFirstResponder 方法。(示例如下)
第一步
创建自定义数据源类。 MyObj 是您希望在选取器中显示项目的类。
在.h中:
#import <UIKit/UIKit.h>
@class myObj;
@interface PickerDataSource : NSObject <UIPickerViewDataSource, UIPickerViewDelegate>
@property (nonatomic, strong) MyObj *selectedObject;
@end
在.m:
#import "PickerDataSource.h"
#import "MyObj.h"
@implementation PickerDataSource
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
self.selectedObject = mySourceArray[row];
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
return mySourceArray.count;
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 1;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
return ((MyObj *)mySourceArray[row]).myTitle;
}
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
int sectionWidth = 300;
return sectionWidth;
}
@end
第二步在您的视图控制器中,导入自定义数据源、委托和设置属性:(您必须包含文本字段委托!)
#import "PickerDataSource.h"
@interface MyViewController () <UIPickerViewDelegate, UITextFieldDelegate>
@property (strong, nonatomic) PickerDataSource *pickerDataSource;
@property (strong, nonatomic) UIPickerView *picker;
@property (strong, nonatomic) UITextField *dummyTextField;
@end
第三步
在您的viewDidLoad 中,调用[self setupPicker];(接下来您将创建此方法)
第四步
创建setupPicker
- (void)setupPicker {
// init custom picker data source
self.pickerDataSource = [PickerDataSource new];
// init custom picker
self.picker = [UIPickerView new];
// set the picker's dataSource and delegate to be your custom data source
self.picker.dataSource = self.pickerDataSource;
self.picker.delegate = self.pickerDataSource;
self.picker.showsSelectionIndicator = YES;
// next step is to write this configure method getting called here
[self configurePickerSubviews];
// lastly, add the dummyTextField to your view.
[self.view addSubview:self.dummyTextField];
}
第五步
创建configurePickerSubviews
- (void)configurePickerSubviews {
// A UIPickerView must be added as an inputView to a UITextField in order to be displayed on button tap
// So you need to create a dummyTextField to do so.
self.dummyTextField = [UITextField new];
// Create a toolbar to add a done button
UIToolbar *toolBar= [[UIToolbar alloc] initWithFrame:CGRectMake(0,0,[UIScreen mainScreen].bounds.size.width,44)];
[toolBar setBarStyle:UIBarStyleDefault];
UIBarButtonItem *done = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStylePlain target:self action:@selector(locationPressed)];
// Create a flex space so that done button will be right aligned
UIBarButtonItem *flex = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
UIBarButtonItem *cancel = [[UIBarButtonItem alloc] initWithTitle:@"Cancel" style:UIBarButtonItemStylePlain target:self action:@selector(dismissPicker)];
toolBar.items = @[cancel, flex, done];
done.tintColor = [UIColor blackColor];
[self.picker addSubview:toolBar];
// Create an input view to add picker + done button as subviews
UIView *inputView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, self.picker.frame.size.height + 44)];
[self.picker setFrame:CGRectMake(0, 0, inputView.frame.size.width, inputView.frame.size.height)];
inputView.backgroundColor = [UIColor clearColor];
[inputView addSubview:self.picker];
[inputView addSubview:toolBar];
// Set custom inputView as container for picker view
self.dummyTextField.inputView = inputView;
// Hiding the textfield will hide the picker
[self.dummyTextField setHidden:YES];
}
第六步
配置PickerDataSource,使其从源数组中获取您想要显示的数据。
第 7 步
点击Run!