iOS 自訂日期和資料來源選擇控制項

來源:互聯網
上載者:User
需求

App開發中經常會有日期選擇(如生日、睡眠定時等)或者省市區選擇等此類功能,通常UI中不會單獨使用UI中的控制項,而是在UIPickerView的基礎上增加一個取消和確定按鈕 特點 支援常見的選擇型的資料格式
該控制項整合了 yyyy-MM-dd、yyyy-MM、hh mm、省市級聯、省市區級聯、自訂資料來源(2列)、自訂資料來源(3列)等多種格式, 即支援UITextField又支援事件觸發機制 即支援XIB也支援純程式碼 效果圖

GitHub:https://github.com/mengday/XXPickerView 整合

首先將XXPickerView檔案夾拖入到工程中 純程式碼(選擇任意一種模式)

#import "ViewController.h"#import "XXTextField.h"#define random(r, g, b, a) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:(a)/255.0]#define RandomColor random(arc4random_uniform(256), arc4random_uniform(256), arc4random_uniform(256), arc4random_uniform(256))@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];    CGFloat x = 170;    CGFloat width = 178;    CGFloat height = 30;    CGFloat margin = 50;    // 模式一    XXTextField *textField = [[XXTextField alloc] init];    textField.frame = CGRectMake(x, 28, width, height);    textField.mode = XXPickerViewModeDate;    textField.backgroundColor = RandomColor;    [self.view addSubview:textField];    // 模式二    XXTextField *textField2 = [[XXTextField alloc] init];    textField2.frame = CGRectMake(x, textField.frame.origin.y + margin, width, height);    textField2.mode = XXPickerViewModeYearAndMonth;    textField2.backgroundColor = RandomColor;    [self.view addSubview:textField2];    // 模式三    XXTextField *textField3 = [[XXTextField alloc] init];    textField3.frame = CGRectMake(x, textField2.frame.origin.y + margin, width, height);    textField3.mode = XXPickerViewModeHourAndMinute;    textField3.backgroundColor = RandomColor;    [self.view addSubview:textField3];    // 模式四    XXTextField *textField4 = [[XXTextField alloc] init];    textField4.frame = CGRectMake(x, textField3.frame.origin.y + margin, width, height);    textField4.mode = XXPickerViewModeProvinceCity;    textField4.backgroundColor = RandomColor;    [self.view addSubview:textField4];    // 模式五    XXTextField *textField5 = [[XXTextField alloc] init];    textField5.frame = CGRectMake(x, textField4.frame.origin.y + margin, width, height);    textField5.mode = XXPickerViewModeProvinceCityAreas;    textField5.backgroundColor = RandomColor;    [self.view addSubview:textField5];    // 模式六    XXTextField *textField6 = [[XXTextField alloc] init];    textField6.frame = CGRectMake(x, textField5.frame.origin.y + margin, width, height);    textField6.mode = XXPickerViewModeDataSourceFor2Column;    textField6.dataSource = [dataSourceFor2Column mutableCopy];    textField6.backgroundColor = RandomColor;    [self.view addSubview:textField6];    // 模式七    XXTextField *textField7 = [[XXTextField alloc] init];    textField7.frame = CGRectMake(x, textField6.frame.origin.y + margin, width, height);;    textField7.mode = XXPickerViewModeDataSourceFor3Column;    textField7.dataSource = [dataSourceFor3Column mutableCopy];    textField7.backgroundColor = RandomColor;    [self.view addSubview:textField7];   }@end
XIB方式

綁定自訂類

拖線並設定模式

@interface ViewController ()@property (weak, nonatomic) IBOutlet XXTextField *textField;@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];    _textField.mode = XXPickerViewModeDate;}@end
事件方式
#import "ViewController.h"#import "XXInputView.h"@interface ViewController ()@property (weak, nonatomic) XXInputView *inputView;@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];}- (IBAction)showClicked:(id)sender {    [self.inputView show];}- (XXInputView *)inputView {    if (_inputView == nil) {        XXInputView *inputView = [[XXInputView alloc] initWithFrame:CGRectMake(0, [UIScreen mainScreen].bounds.size.height, [UIScreen mainScreen].bounds.size.width, 200) mode:XXPickerViewModeDate dataSource:nil];        inputView.hideSeparator = YES;        inputView.completeBlock = ^(NSString *dateString){            NSLog(@"selected data : %@", dateString);        };        [self.view addSubview:inputView];        self.inputView = inputView;    }    return _inputView;}@end
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.