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

089使用选择器视图

时间:2015-06-15 21:56:17      阅读:180      评论:0      收藏:0      [点我收藏+]

标签:

效果如下:

技术分享

ViewController.h

1 #import <UIKit/UIKit.h>
2 
3 @interface ViewController : UIViewController<UIPickerViewDataSource, UIPickerViewDelegate>
4 @property (strong, nonatomic) UIPickerView *pikVCustom;
5 @property (strong, nonatomic) NSDictionary *dicSongInfo;
6 @property (strong, nonatomic) NSArray *arrSinger;
7 @property (strong, nonatomic) NSArray *arrSong;
8 
9 @end

ViewController.m

  1 #import "ViewController.h"
  2 
  3 @interface ViewController ()
  4 - (void)layoutUI;
  5 - (void)buttonDidPush:(UIButton *)sender;
  6 - (void)loadData;
  7 @end
  8 
  9 @implementation ViewController
 10 #define singerComponentIndex 0
 11 #define songComponentIndex 1
 12 
 13 - (void)viewDidLoad {
 14     [super viewDidLoad];
 15     
 16     [self layoutUI];
 17 }
 18 
 19 - (void)didReceiveMemoryWarning {
 20     [super didReceiveMemoryWarning];
 21     // Dispose of any resources that can be recreated.
 22 }
 23 
 24 - (void)layoutUI {
 25     _pikVCustom = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
 26     CGPoint newPoint = self.view.center;
 27     _pikVCustom.center = newPoint;
 28     _pikVCustom.dataSource = self;
 29     _pikVCustom.delegate = self;
 30     [self.view addSubview:_pikVCustom];
 31     
 32     UIButton *btnChoice = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 150, 40)];
 33     newPoint.y += 160;
 34     btnChoice.center = newPoint;
 35     btnChoice.layer.masksToBounds = YES;
 36     btnChoice.layer.cornerRadius = 10.0;
 37     btnChoice.layer.borderColor = [UIColor blackColor].CGColor;
 38     btnChoice.layer.borderWidth = 2.0;
 39     [btnChoice setTitle:@"选择完毕" forState:UIControlStateNormal];
 40     [btnChoice setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
 41     [btnChoice addTarget:self
 42                   action:@selector(buttonDidPush:)
 43         forControlEvents:UIControlEventTouchUpInside];
 44     [self.view addSubview:btnChoice];
 45     
 46     [self loadData];
 47 }
 48 
 49 - (void)loadData {
 50     //读取SongInfo.plist文件内容
 51     NSBundle *bundle = [NSBundle mainBundle];
 52     NSURL *urlSongInfo = [bundle URLForResource:@"SongInfo" withExtension:@"plist"];
 53     _dicSongInfo = [NSDictionary dictionaryWithContentsOfURL:urlSongInfo];
 54     _arrSinger = [_dicSongInfo allKeys];
 55     _arrSinger = [_arrSinger sortedArrayUsingSelector:@selector(compare:)];
 56     _arrSong = [_dicSongInfo objectForKey:_arrSinger[0]];
 57 }
 58 
 59 - (void)buttonDidPush:(UIButton *)sender {
 60     NSInteger selectedRowOfSinger = [_pikVCustom selectedRowInComponent:singerComponentIndex];
 61     NSInteger selectedRowOfSong = [_pikVCustom selectedRowInComponent:songComponentIndex];
 62     NSString *strMessage =
 63     [[NSString alloc] initWithFormat:@"您选择了%@的《%@》",
 64      _arrSinger[selectedRowOfSinger],
 65      _arrSong[selectedRowOfSong]];
 66     UIAlertView *alertV = [[UIAlertView alloc] initWithTitle:@"选择的歌曲信息"
 67                                                      message:strMessage
 68                                                     delegate:nil
 69                                            cancelButtonTitle:nil
 70                                            otherButtonTitles:@"确定", nil];
 71     [alertV show];
 72 }
 73 
 74 #pragma mark - PickerView
 75 - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
 76     return 2; //返回组件列数
 77 }
 78 
 79 - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
 80     NSInteger rowCount = 0;
 81     switch (component) {
 82         case singerComponentIndex:
 83             rowCount = [_arrSinger count];
 84             break;
 85         case songComponentIndex:
 86             rowCount = [_arrSong count];
 87             break;
 88     }
 89     return rowCount; //返回组件行数
 90 }
 91 
 92 - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
 93     NSString *strTitle = @"";
 94     switch (component) {
 95         case singerComponentIndex:
 96             strTitle = _arrSinger[row];
 97             break;
 98         case songComponentIndex:
 99             strTitle = _arrSong[row];
100             break;
101     }
102     return strTitle;
103 }
104 
105 - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
106     //如果选取的是第一个选取器时
107     if (component == singerComponentIndex) {
108         _arrSong = [_dicSongInfo objectForKey:_arrSinger[row]];
109         
110         //重新装载第二个选取器内容
111         [_pikVCustom reloadComponent:songComponentIndex];
112         [_pikVCustom selectRow:0 inComponent:songComponentIndex animated:YES];
113     }
114 }
115 
116 - (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
117     CGFloat width = 0;
118     switch (component) {
119         case singerComponentIndex:
120             width = 120.0;
121             break;
122         case songComponentIndex:
123             width = 200.0;
124             break;
125     }
126     return width;
127 }
128 
129 @end

SongInfo.plist

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
 3 <plist version="1.0">
 4 <dict>
 5     <key>许嵩</key>
 6     <array>
 7         <string>断桥残雪</string>
 8         <string>玫瑰花的葬礼</string>
 9         <string>城府</string>
10         <string>清明雨上</string>
11     </array>
12     <key>周杰伦</key>
13     <array>
14         <string>听妈妈的话</string>
15         <string>双节棍</string>
16         <string>菊花台</string>
17         <string>青花瓷</string>
18         <string>千里之外</string>
19         <string>蜗牛</string>
20         <string>夜曲</string>
21     </array>
22     <key>梁静茹</key>
23     <array>
24         <string>可惜不是你</string>
25         <string>勇气</string>
26         <string></string>
27         <string>没有如果</string>
28     </array>
29     <key>阿杜</key>
30     <array>
31         <string>离别</string>
32         <string>差一点</string>
33     </array>
34     <key>林俊杰</key>
35     <array>
36         <string>江南</string>
37         <string>曹操</string>
38         <string>一千年以后</string>
39     </array>
40     <key>张靓颖</key>
41     <array>
42         <string>画心</string>
43         <string>好不容易</string>
44     </array>
45     <key>方大同</key>
46     <array>
47         <string>黑白</string>
48         <string>三人游</string>
49         <string>love song</string>
50     </array>
51     <key>凤凰传奇</key>
52     <array>
53         <string>最炫名族风</string>
54         <string>荷塘月色</string>
55     </array>
56     <key>邱永传</key>
57     <array>
58         <string>十一年</string>
59     </array>
60     <key>胡夏</key>
61     <array>
62         <string>伤心童话</string>
63     </array>
64 </dict>
65 </plist>

 

089使用选择器视图

标签:

原文地址:http://www.cnblogs.com/huangjianwu/p/4579256.html

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