-
StackOverflow 文件
-
iOS 教程
-
UIPickerView
-
基本的例子
迅速
class PickerViewExampleViewController : UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {
@IBOutlet weak var btnFolder: UIButton!
let pickerView = UIPickerView()
let pickerViewRows = ["First row,", "Secound row,","Third row,","Fourth row"]
override func viewDidLoad() {
super.viewDidLoad()
self.btnFolder.addTarget(self, action: #selector(CreateListVC.btnFolderPress), forControlEvents: UIControlEvents.TouchUpInside)
}
@objc private func btnFolderPress() {
self.pickerView.delegate = self
self.pickerView.dataSource = self
self.view.addSubview(self.pickerView)
}
//MARK: UIPickerViewDelegate
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return self.pickerViewRows[row]
}
//MARK: UIPickerViewDataSource
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return self.pickerViewRows.count
}
}
Objective-C
@property (nonatomic,strong) UIPickerView *countryPicker;
@property (nonatomic,strong) NSArray *countryNames;
- (void)viewDidLoad {
[super viewDidLoad];
_countryNames = @[@"Australia (AUD)", @"China (CNY)",
@"France (EUR)", @"Great Britain (GBP)", @"Japan (JPY)",@"INDIA (IN)",@"AUSTRALIA (AUS)",@"NEW YORK (NW)"];
[self pickcountry];
}
-(void)pickcountry {
_countryPicker = [[UIPickerView alloc]init];
_countryPicker.delegate = self;
_countryPicker.dataSource = self;
[[UIPickerView appearance] setBackgroundColor:[UIColor colorWithRed:21/255.0 green:17/255.0 blue:50/255.0 alpha:1.0]];
}
#pragma mark- pickerView Delegates And datasource
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
return _countryNames.count;
}
- (NSString *)pickerView:(UIPickerView *)pickerView
titleForRow:(NSInteger)row
forComponent:(NSInteger)component {
return _countryNames[row];
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
NSString *pickedCountryName = _countryNames[row];
}