年年有"余"

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 2568|回复: 0

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

[复制链接]
  • TA的每日心情

    2024-10-15 10:05
  • 签到天数: 372 天

    [LV.9]以坛为家II

    发表于 2014-9-29 13:55:02 | 显示全部楼层 |阅读模式
    1、先上效果图:
    iOS 模拟器屏幕快照“2014年9月29日 下午1.37.40”.png     iOS 模拟器屏幕快照“2014年9月29日 下午1.39.44”.png

    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
    复制代码

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    手机版|小黑屋|Archiver|iOS开发笔记 ( 湘ICP备14010846号 )

    GMT+8, 2024-12-22 16:44 , Processed in 0.047170 second(s), 21 queries .

    Powered by Discuz! X3.4

    Copyright © 2001-2021, Tencent Cloud.

    快速回复 返回顶部 返回列表