• iOS:UIPickerView选择器的使用


    通过UIPickerView选择器做的一个类似于密码锁的日期时间表

       源码如下:

     1 #import <UIKit/UIKit.h>
     2 
     3 @interface ViewController : UIViewController<UIPickerViewDataSource,UIPickerViewDelegate>
     4 @property (strong,nonatomic) NSArray *weeks;//显示第一列的数据
     5 @property (strong,nonatomic) NSArray *time; //显示第二列的数据
     6 
     7 @property (strong,nonatomic)NSArray *years;   //显示第三列的显示年
     8 @property (strong,nonatomic)NSArray *months;  //显示第四列的显示月
     9 @property (strong,nonatomic)NSArray *days;    //显示第五列的显示日
    10 
    11 @property (assign,nonatomic)NSInteger number;
    12 @property (strong,nonatomic)NSString *str;
    13 @end
      1 #import "ViewController.h"
      2 
      3 @interface ViewController ()
      4 @property (weak, nonatomic) IBOutlet UIPickerView *pickerView;
      5 
      6 @end
      7 
      8 @implementation ViewController
      9 - (void)viewDidLoad {
     10     [super viewDidLoad];
     11     
     12     //初始化
     13     
     14     self.weeks = [NSArray arrayWithObjects:@"Mon",@"Tus",@"Thi",@"Wen",@"Fir",@"Sat",@"Sun", nil];
     15     
     16     self.time = [NSArray arrayWithObjects:@"上午",@"中午",@"下午", nil];
     17     
     18     self.years = [NSArray arrayWithObjects:@"2000",@"2001",@"2002",@"2003",@"2004",@"2005",@"2006",@"2007",@"2008",@"2009",@"2010",@"2011",@"2012",@"2013",@"2014",nil];
     19     
     20     self.months = [NSArray arrayWithObjects:@"1",@"2", @"3", @"4", @"5", @"6", @"7", @"8", @"9", @"10", @"11", @"12", nil];
     21     
     22     self.days = [NSArray arrayWithObjects:@"01",@"02",@"03",@"04",@"05",@"06",@"07",@"08",@"09",@"10",@"11",@"12",@"13",@"14",@"15",@"16",@"17",@"18",@"19",@"20",@"21",@"22",@"23",@"24",@"25",@"26",@"27",@"28",@"29",@"30",@"31",nil];
     23     
     24     
     25     //设置pickerView的DataSource和Delegate
     26     self.pickerView.dataSource = self;
     27     self.pickerView.delegate = self;
     28 }
     29 - (void)didReceiveMemoryWarning {
     30     [super didReceiveMemoryWarning];
     31     // Dispose of any resources that can be recreated.
     32 }
     33 
     34 #pragma mark-pickerView的代理方法
     35 
     36 //设置有多少列
     37 -(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
     38 {
     39     return 5;
     40 }
     41 
     42 //设置每列多少行
     43 -(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
     44 {
     45     switch (component)
     46     {
     47         case 0:
     48             self.number = self.weeks.count;
     49             break;
     50         case 1:
     51             self.number = self.time.count;
     52             break;
     53         case 2:
     54             self.number = self.years.count;
     55             break;
     56         case 3:
     57             self.number = self.months.count;
     58             break;
     59         case 4:
     60             self.number = self.days.count;
     61             break;
     62     }
     63     
     64     return self.number;
     65     
     66 }
     67 
     68 //设置每行每列显示的内容
     69 -(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
     70 {
     71     
     72     switch (component)
     73     {
     74         case 0:
     75             self.str = self.weeks[row];
     76             break;
     77         case 1:
     78             self.str = self.time[row];
     79             break;
     80         case 2:
     81             self.str = self.years[row];
     82             break;
     83         case 3:
     84             self.str = self.months[row];
     85             break;
     86         case 4:
     87             self.str = self.days[row];
     88             break;
     89     }
     90 
     91     return self.str;
     92     
     93 }
     94 
     95 //通过代理方法didSelectRows获取数据
     96 - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
     97 {
     98     //NSLog(@"列:%ld,行:%ld",component,row);   
     99     switch (component)
    100     {
    101         case 0:
    102             NSLog(@"%@",[NSString stringWithFormat:@"%@,%@,%@,%@,%@",self.weeks[row],self.time[[pickerView selectedRowInComponent:1]],self.years[[pickerView selectedRowInComponent:2]],self.months[[pickerView selectedRowInComponent:3]],self.days[[pickerView selectedRowInComponent:4]]]);
    103             break;
    104         case 1:
    105             NSLog(@"%@",[NSString stringWithFormat:@"%@,%@,%@,%@,%@",self.weeks[[pickerView selectedRowInComponent:0]],self.time[row],self.years[[pickerView selectedRowInComponent:2]],self.months[[pickerView selectedRowInComponent:3]],self.days[[pickerView selectedRowInComponent:4]]]);
    106             break;
    107         case 2:
    108             NSLog(@"%@",[NSString stringWithFormat:@"%@,%@,%@,%@,%@",self.weeks[[pickerView selectedRowInComponent:0]],self.time[[pickerView selectedRowInComponent:1]],self.years[row],self.months[[pickerView selectedRowInComponent:3]],self.days[[pickerView selectedRowInComponent:4]]]);
    109             break;
    110         case 3:
    111             NSLog(@"%@",[NSString stringWithFormat:@"%@,%@,%@,%@,%@",self.weeks[[pickerView selectedRowInComponent:0]],self.time[[pickerView selectedRowInComponent:1]],self.years[[pickerView selectedRowInComponent:2]],self.months[row],self.days[[pickerView selectedRowInComponent:4]]]);
    112             break;
    113         case 4:
    114             NSLog(@"%@",[NSString stringWithFormat:@"%@,%@,%@,%@,%@",self.weeks[[pickerView selectedRowInComponent:0]],self.time[[pickerView selectedRowInComponent:1]],self.years[[pickerView selectedRowInComponent:2]],self.months[[pickerView selectedRowInComponent:3]],self.days[row]]);
    115             break;
    116     }
    117 }
    118 @end
  • 相关阅读:
    [LeetCode] 493. Reverse Pairs 翻转对
    [LeetCode] Super Washing Machines 超级洗衣机
    [LeetCode] Perfect Number 完美数字
    [LeetCode] 483. Smallest Good Base 最小的好基数
    [LeetCode] Sliding Window Median 滑动窗口中位数
    [LeetCode] Diameter of Binary Tree 二叉树的直径
    [LeetCode] 01 Matrix 零一矩阵
    [LeetCode] Convert BST to Greater Tree 将二叉搜索树BST转为较大树
    [LeetCode] Reverse String II 翻转字符串之二
    [LeetCode] Minimum Time Difference 最短时间差
  • 原文地址:https://www.cnblogs.com/XYQ-208910/p/4764454.html
Copyright © 2020-2023  润新知