以下为相关代码,仅供参考:
#import <UIKit/UIKit.h> typedef void (^ReturnCityBlock)(NSString *string,int provinceIndex,int cityIndex); @interface PickerBgView : UIView<UIPickerViewDelegate, UIPickerViewDataSource> @property (weak, nonatomic) IBOutlet UIButton *doneBtn; @property (weak, nonatomic) IBOutlet UIButton *closeBtn; @property (weak, nonatomic) IBOutlet UIPickerView *pickerView; @property (weak, nonatomic) IBOutlet UIView *clearView; @property (nonatomic, copy)ReturnCityBlock returnBlock; @property (nonatomic, assign)int cityIndex;//城市 @property (nonatomic, assign)int provinceIndex;//省份 - (id)initWithReturnBlock:(ReturnCityBlock)block; - (void)showInView:(UIView *)view; - (void)cancelPicker; @end
#import "PickerBgView.h" @implementation PickerBgView{ NSArray *provinces, *cities; NSString *_state; NSString *_city; } - (id)initWithReturnBlock:(ReturnCityBlock)block{ self = [[[NSBundle mainBundle] loadNibNamed:@"PickerBgView" owner:self options:nil] objectAtIndex:0]; if (self) { self.pickerView.delegate = self; self.pickerView.dataSource = self; provinces = [[NSArray alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"city.plist" ofType:nil]]; [self.closeBtn addTarget:self action:@selector(cancelPickerViewAction:) forControlEvents:UIControlEventTouchUpInside]; self.returnBlock = block; UITapGestureRecognizer *closeTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(cancelGestureAction)]; [self.clearView addGestureRecognizer:closeTap]; [self.doneBtn addTarget:self action:@selector(doneBtnAction) forControlEvents:UIControlEventTouchUpInside]; } return self; } #pragma mark - PickerView lifecycle - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView { return 2; } - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component { switch (component) { case 0: return [provinces count]; break; case 1: return [cities count]; break; default: return 0; break; } } - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component { if (component == 0) { return [[provinces objectAtIndex:row] objectForKey:@"state"]; }else{ return [cities objectAtIndex:row]; } } - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component { switch (component) { case 0: _cityIndex = 0; cities = [[provinces objectAtIndex:row] objectForKey:@"cities"]; [self.pickerView selectRow:_cityIndex inComponent:1 animated:YES]; [self.pickerView reloadComponent:1]; _state = [[provinces objectAtIndex:row] objectForKey:@"state"]; _city = [cities objectAtIndex:_cityIndex]; _provinceIndex = (int)row; break; case 1: _city = [cities objectAtIndex:row]; _cityIndex = (int)row; break; default: break; } } - (void)doneBtnAction{ self.returnBlock([NSString stringWithFormat:@"%@%@",_state,_city],_provinceIndex,_cityIndex); [self cancelPicker]; } #pragma mark - animation - (void)showInView:(UIView *) view { self.frame = CGRectMake(0, ScreenHeight, ScreenWidth, ScreenHeight); //默认已选中省份城市 cities = [[provinces objectAtIndex:_provinceIndex] objectForKey:@"cities"]; _state = [[provinces objectAtIndex:_provinceIndex] objectForKey:@"state"]; _city = [cities objectAtIndex:_cityIndex]; [self.pickerView selectRow:_provinceIndex inComponent:0 animated:YES]; [self.pickerView selectRow:_cityIndex inComponent:1 animated:YES]; [self.pickerView reloadAllComponents]; [view addSubview:self]; [UIView animateWithDuration:0.3 animations:^{ self.frame = CGRectMake(0, 0, ScreenWidth, ScreenHeight); }]; } - (void)cancelPicker { [UIView animateWithDuration:0.3 animations:^{ self.frame = CGRectMake(0, self.frame.origin.y+self.frame.size.height, self.frame.size.width, self.frame.size.height); } completion:^(BOOL finished){ [self removeFromSuperview]; }]; } - (void)cancelGestureAction { [self cancelPicker]; } #pragma mark - cancel和ok -(void)cancelPickerViewAction:(UIButton *)sender { [self cancelPicker]; } @end