分享

自定义PickView,一行代码即可搞定选择器!

 爽行天下丶 2015-06-26


1、先上效果图:
   

2、使用说明:
该控件可以广泛应用于各种选择场景,这里以TextField为例,只需要一行代码即可在需要选择控件的TextField中应用
  1. [SAPickView pickViewWithArray:array forTextField:textField];
复制代码
轻松搞定!

3、源代码
SAPickView.h
  1. //
  2. //  SAPickView.h
  3. //
  4. //  Created by Sian on 14-9-29.
  5. //  Copyright (c) 2014年 Sian. All rights reserved.
  6. //

  7. #import <UIKit/UIKit.h>

  8. @interface SAPickView : UIView

  9. @property (nonatomic, strong)   UIPickerView    *pickView;

  10. @property (nonatomic, strong)   NSArray         *dataArray;

  11. @property (nonatomic, strong)   UITextField     *textField;

  12. - (id)initWithArray:(NSArray *)array;

  13. + (void)pickViewWithArray:(NSArray *)array forTextField:(UITextField *)textField;

  14. - (void)show;

  15. @end
复制代码


SAPickView.m
  1. //
  2. //  SAPickView.m
  3. //
  4. //  Created by Sian on 14-9-29.
  5. //  Copyright (c) 2014年 Sian. All rights reserved.
  6. //

  7. #import "SAPickView.h"

  8. @interface SAPickView () <UIPickerViewDataSource, UIPickerViewDelegate>
  9. {
  10.     UIToolbar   *_toolBar;
  11.     CGSize      _size;
  12. }
  13. @end
  14. @implementation SAPickView

  15. - (id)initWithArray:(NSArray *)array
  16. {
  17.     if (self = [super init]) {
  18.         self.alpha = 0;
  19.         self.dataArray = array;
  20.         _size = [[UIScreen mainScreen] bounds].size;
  21.         self.backgroundColor = [UIColor clearColor];
  22.         [self addsubViews];
  23.     } return self;
  24. }

  25. - (void)addsubViews
  26. {
  27.     self.frame = (CGRect){CGPointZero, _size};
  28.     [[[[UIApplication sharedApplication] delegate] window] addSubview:self];
  29.    
  30.     _toolBar = [[UIToolbar alloc] init];
  31.     _toolBar.tintColor = kGrayColor(1.0);
  32.     _toolBar.frame = (CGRect){0, _size.height, _size.width, 44};
  33.     [self addSubview:_toolBar];
  34.    
  35.     // 按钮自定义
  36.     UIBarButtonItem *item1 = [UIBarButtonItem barButtonItemWithTitle:@"取消" addTarget:self action:@selector(dismiss)];
  37.     UIBarButtonItem *item2 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
  38.     UIBarButtonItem *item3 = [UIBarButtonItem barButtonItemWithTitle:@"确定" addTarget:self action:@selector(done)];
  39.     _toolBar.items = [NSArray arrayWithObjects:item1, item2, item3, nil];
  40.    
  41.     self.pickView = [[UIPickerView alloc] init];
  42.     self.pickView.showsSelectionIndicator = YES;
  43.     self.pickView.backgroundColor = kGrayColor(1.0);
  44.     self.pickView.dataSource = self;
  45.     self.pickView.delegate = self;
  46.     self.pickView.frame = (CGRect){0, _size.height + 44, _size.width, 206 - 44};
  47.     [self addSubview:self.pickView];
  48. }

  49. + (void)pickViewWithArray:(NSArray *)array forTextField:(UITextField *)textField
  50. {
  51.     SAPickView *pickView = [[self alloc] initWithArray:array];
  52.     pickView.textField = textField;
  53.     [pickView show];
  54. }

  55. - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
  56. {
  57.     return 1;
  58. }

  59. - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
  60. {
  61.     return self.dataArray.count;
  62. }

  63. - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
  64. {
  65.     return self.dataArray[row];
  66. }

  67. - (void)show
  68. {
  69.     [UIView animateWithDuration:0.3 animations:^{
  70.         self.alpha = 1;
  71.         _toolBar.frame = (CGRect){0, _size.height - 206, _size.width, 44};
  72.         self.pickView.frame = (CGRect){0, _size.height - 206 + 44, _size.width, 206 - 44};
  73.     }];
  74. }

  75. - (void)dismiss
  76. {
  77.     [UIView animateWithDuration:0.3 animations:^{
  78.         self.alpha = 0;
  79.         _toolBar.frame = (CGRect){0, _size.height, _size.width, 44};
  80.         self.pickView.frame = (CGRect){0, _size.height + 44, _size.width, 206 - 44};
  81.     } completion:^(BOOL finished) {
  82.         [self removeFromSuperview];
  83.     }];
  84. }

  85. - (void)done
  86. {
  87.     self.textField.text = [self.dataArray objectAtIndex:[self.pickView selectedRowInComponent:0]];
  88.     [self dismiss];
  89. }
  90. @end
复制代码


    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多