码迷,mamicode.com
首页 > 其他好文 > 详细

UIPickerView

时间:2016-07-13 13:58:31      阅读:205      评论:0      收藏:0      [点我收藏+]

标签:

1.UIPickView什么时候用?

通常在注册模块,当用户需要选择一些东西的时候,比如说城市,往往
弹出一个PickerView给他们选择。
 

UIPickView常见用法,演示实例程序

1> 独立的,没有任何关系 => 菜单系统。
2> 相关联的,下一列和第一列有联系=> 省会城市选择
3> 图文并帽, => 国旗选择

 

4.UIDatePicker什么时候用?
当用户选择日期的时候,一般弹出一个UIDatePicker给用户选择

 

技术分享
 1 //
 2 //  ViewController.m
 3 //  01-点餐系统
 4 //
 5 //  Created by xiaomage on 15/6/9.
 6 //  Copyright (c) 2015年 xiaomage. All rights reserved.
 7 //
 8 
 9 #import "ViewController.h"
10 
11 @interface ViewController ()<UIPickerViewDelegate,UIPickerViewDataSource>
12 
13 @property (weak, nonatomic) IBOutlet UIPickerView *pickerView;
14 
15 @end
16 
17 @implementation ViewController
18 
19 - (void)viewDidLoad {
20     
21     [super viewDidLoad];
22     // Do any additional setup after loading the view, typically from a nib.
23   //设置代理方法方式如下,还有拖线,  
24     self.pickerView.delegate = self;
25     
26 }
27 
28 
29 // 返回pickerView有多少列
30 - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
31 {
32     return 3;
33 }
34 
35 // 返回第component列有多少行
36 - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
37 {
38     return 2;
39 }
40 
41 
42 #pragma mark - 代理
43 // 返回第component列的每一行的行高
44 - (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
45 {
46     return 80.0;
47 }
48 
49 // 返回第component列第row行的标题
50 - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
51 {
52     return @"a";
53 }
54 
55 // NSAttributedString富文本属性: 可以描述文字大小和颜色
56 //- (NSAttributedString *)pickerView:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component NS_AVAILABLE_IOS(6_0); // attributed title is favored if both methods are implemented
57 
58 
59 // 总结:如果同时实现返回字符串和view的方法,返回UIView的优先级比较高
60 // 返回第component列第row行的View
61 //- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
62 //{
63 //    UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 60, 60)];
64 //    
65 //    v.backgroundColor = [UIColor redColor];
66 //    
67 //    return v;
68 //}
69 
70 // 选中第component第row的时候调用
71 // __func__: 返回当前方法在哪个类里面调用
72 - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
73 {
74     NSLog(@"%s---%ld-%ld",__func__,component,row);
75 }
76 
77 
78 @end
View Code

 

技术分享
  1 //
  2 //  ViewController.m
  3 //  01-点餐系统
  4 //
  5 //  Created by xiaomage on 15/6/9.
  6 //  Copyright (c) 2015年 xiaomage. All rights reserved.
  7 //
  8 
  9 #import "ViewController.h"
 10 
 11 // 分屏:cmd + option + return
 12 
 13 // 退出分屏:cmd + return
 14 
 15 @interface ViewController ()<UIPickerViewDelegate,UIPickerViewDataSource>
 16 @property (weak, nonatomic) IBOutlet UILabel *frultLabel;
 17 @property (weak, nonatomic) IBOutlet UILabel *mainLabel;
 18 @property (weak, nonatomic) IBOutlet UILabel *drinkLabel;
 19 
 20 @property (weak, nonatomic) IBOutlet UIPickerView *pickerView;
 21 
 22 
 23 @property (nonatomic, strong) NSArray *foods;
 24 
 25 @end
 26 
 27 @implementation ViewController
 28 
 29 // 点击随机的时候调用
 30 - (IBAction)random:(UIButton *)sender {
 31     
 32     // pickerView每一列随机选中一行
 33     
 34     // 随机选中的文字展示到label
 35     
 36     // cmd + option + [ 代码上跳
 37     // cmd + [ 代码左移
 38     for (int i = 0; i < 3; i++) {
 39         
 40         // 假设让第0列随机选中一行
 41         // 取出第0列的行数
 42         NSInteger count = [self.foods[i] count];
 43         
 44         int random = arc4random_uniform((u_int32_t)count);
 45         // 不会触发代理的选中方法
 46         [_pickerView selectRow:random inComponent:i animated:YES];
 47         
 48         // 主动给label赋值
 49         [self pickerView:nil didSelectRow:random inComponent:i];
 50     }
 51     
 52 }
 53 
 54 - (NSArray *)foods
 55 {
 56     if (_foods == nil) {
 57         
 58         // 加载Pilst文件
 59        NSString *filePath = [[NSBundle mainBundle] pathForResource:@"foods.plist" ofType:nil];
 60         
 61         // 大数组:pickerView有多少列
 62         _foods = [NSArray arrayWithContentsOfFile:filePath];
 63         
 64     }
 65     
 66     return _foods;
 67 }
 68 
 69 - (void)viewDidLoad {
 70     
 71     [super viewDidLoad];
 72     // Do any additional setup after loading the view, typically from a nib.
 73     
 74     self.pickerView.delegate = self;
 75     
 76     // 初始化label标题
 77     
 78     for (int i = 0; i < 3; i++) {
 79         
 80         [self pickerView:nil didSelectRow:0 inComponent:i];
 81         
 82     }
 83     
 84 }
 85 // 返回pickerView有多少列
 86 - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
 87 {
 88     return self.foods.count;
 89 }
 90 
 91 // 返回第component列有多少行
 92 - (NSInteger)r :(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
 93 {
 94     return [self.foods[component] count];
 95 }
 96 
 97 // 返回第component列第row行的标题
 98 - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
 99 {
100     return self.foods[component][row];
101 }
102 
103 
104 - (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
105 {
106     return 45;
107 }
108 
109 // 给label赋值
110 // 选中第component列第row行的时候调用
111 // 注意:这个方法必须用户主动拖动pickerView,才会调用
112 - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
113 {
114     
115     switch (component) {
116         case 0:
117             // 设置水果
118             _frultLabel.text = self.foods[component][row];
119             break;
120         case 1:
121             // 设置主食
122             _mainLabel.text = self.foods[component][row];
123             break;
124         case 2:
125             // 设置饮料
126             _drinkLabel.text = self.foods[component][row];
127             break;
128     }
129     
130     
131 }
132 
133 
134 @end
View Code

 

UIPickerView

标签:

原文地址:http://www.cnblogs.com/jiaozi-li/p/5666550.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!