ShowGenderAlertView.m 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. //
  2. // ShowGenderAlertView.m
  3. // smartRhino
  4. //
  5. // Created by armin on 2019/11/1.
  6. // Copyright © 2019 tederen. All rights reserved.
  7. //
  8. #import "ShowGenderAlertView.h"
  9. @interface ShowGenderAlertView ()<UIPickerViewDelegate,UIPickerViewDataSource>
  10. @property (copy, nonatomic) void(^confirmBlock)(NSString *gender);
  11. @property (copy, nonatomic) void(^cancelBlock)(void);
  12. @property (strong,nonatomic) IBOutlet UIPickerView *pickView;
  13. @property (strong,nonatomic) NSString *selectGender;
  14. @property (strong,nonatomic) IBOutlet NSLayoutConstraint *bottomH;
  15. @end
  16. @implementation ShowGenderAlertView
  17. static UINib *ViewNib = nil;
  18. + (instancetype)selectView{
  19. if (ViewNib == nil) {
  20. ViewNib = [UINib nibWithNibName:@"ShowGenderAlertView" bundle:nil];
  21. }
  22. ShowGenderAlertView *alert = [[ViewNib instantiateWithOwner:nil options:nil] lastObject];
  23. return alert;
  24. }
  25. /**
  26. * 弹出选择性别
  27. */
  28. +(instancetype)initShowGenderAlertViewWithGender:(NSString *)genderStr
  29. confirm:(void (^)(NSString * _Nonnull))confirmBlock
  30. cancle:(void (^)(void))cancleBlock{
  31. ShowGenderAlertView *alert = [self selectView];
  32. [alert setFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
  33. alert.bottomH.constant = 284 + kiphoneXBootomHeight;
  34. //初始默认值
  35. alert.selectGender = genderStr;
  36. alert.pickView.delegate = alert;
  37. alert.pickView.dataSource = alert;
  38. if([genderStr isEqualToString:@"男"]){
  39. [alert.pickView selectRow:0 inComponent:0 animated:NO];
  40. }else if([genderStr isEqualToString:@"女"]){
  41. [alert.pickView selectRow:1 inComponent:0 animated:NO];
  42. }
  43. [alert setStatusBarStyle:UIStatusBarStyleDefault];
  44. [alert setGesture:YES];
  45. [alert setConfirmBlock:confirmBlock];
  46. [alert setCancelBlock:cancleBlock];
  47. return alert;
  48. }
  49. -(IBAction)userDidBackClickAction:(id)sender{
  50. [self dismissWithAnimation:kAlertAnimationBottom];
  51. if(self.cancelBlock){
  52. self.cancelBlock();
  53. }
  54. }
  55. -(IBAction)userDidSureClickAction:(id)sender{
  56. [self dismissWithAnimation:kAlertAnimationBottom];
  57. if(self.confirmBlock){
  58. self.confirmBlock(self.selectGender);
  59. }
  60. }
  61. #pragma mark - dataSouce
  62. //有几行
  63. - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
  64. return 1;
  65. }
  66. //行中有几列
  67. - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
  68. return 2;
  69. }
  70. //列显示的数据
  71. - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger) row forComponent:(NSInteger)component {
  72. if(row == 0){
  73. return @"男";
  74. }
  75. return @"女";
  76. }
  77. #pragma mark - delegate
  78. // 选中某一组的某一行
  79. - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
  80. switch (row) {
  81. case 0:
  82. self.selectGender = @"男";
  83. break;
  84. case 1:
  85. self.selectGender = @"女";
  86. break;
  87. default:
  88. break;
  89. }
  90. }
  91. @end