#import <UIKit/UIKit.h>
@interface SelectPickerView : UIView
@property (weak, nonatomic) IBOutlet UIView *view;
@property (weak, nonatomic) IBOutlet UILabel *label_title;
/** 外界数据源数组 */
@property (nonatomic,strong)NSArray * pickerSourceAy; // 要展示的地区
typedef void (^PickSuccessBlock)(NSInteger arrIndex);
/** 成功时回调 */
@property (nonatomic,strong)PickSuccessBlock pickSuccessBlock;
+ (instancetype)regionView;
/
#import "SelectPickerView.h"
@interface SelectPickerView ()<UIPickerViewDataSource,UIPickerViewDelegate>
@property (weak, nonatomic) IBOutlet UIPickerView *pickerView;
/** 选中的行索引 */
@property (nonatomic,assign)NSInteger selectedIndex;
@end
@implementation SelectPickerView
+ (instancetype)regionView {
return [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self) owner:nil options:nil] firstObject];
}
- (IBAction)but_Select:(id)sender {
self.pickSuccessBlock(self.selectedIndex);
[self removeFromSuperview];
}
- (IBAction)but_Cancel:(id)sender {
[self removeFromSuperview];
}
#pragma Mark -- UIPickerViewDataSource
// pickerView 列数
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 1;
}
// pickerView 每列个数
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
return [self.pickerSourceAy count];
}
#pragma Mark -- UIPickerViewDelegate
// 每列宽度
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
// if (component == 1) {
// return 40;
// }
return 270;
}
// 返回选中的行
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
self.selectedIndex = row;
}
//返回当前行的内容,此处是将数组中数值添加到滚动的那个显示栏上
-(NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return [self.pickerSourceAy objectAtIndex:row];
}
接下来调用
SelectPickerView * selectPicker = [SelectPickerView regionView];
selectPicker.frame = self.view.bounds;
selectPicker.backgroundColor = [UIColor clearColor];
selectPicker.view.layer.masksToBounds = YES;
selectPicker.view.layer.cornerRadius = 5;
[self.view addSubview:selectPicker];
NSArray * AyPicker = @[@"1",@"2",.....];
selectPicker.pickerSourceAy = AyPicker;
selectPicker.pickSuccessBlock = ^(NSInteger Index) {
NSString* str = AyPicker[Index];
}