UIPickerView简单应用
2016-03-11 21:28
555 查看
下面是一些效果图
![](http://images2015.cnblogs.com/blog/868060/201603/868060-20160311212544397-322277998.png)
![](http://images2015.cnblogs.com/blog/868060/201603/868060-20160311212554085-605443562.png)
![](http://images2015.cnblogs.com/blog/868060/201603/868060-20160311212605944-973919412.png)
下面是代码。有些枯燥 , 其实并不难 。
![](http://images2015.cnblogs.com/blog/868060/201603/868060-20160311212544397-322277998.png)
![](http://images2015.cnblogs.com/blog/868060/201603/868060-20160311212554085-605443562.png)
![](http://images2015.cnblogs.com/blog/868060/201603/868060-20160311212605944-973919412.png)
下面是代码。有些枯燥 , 其实并不难 。
#import <UIKit/UIKit.h> @interface ViewController : UIViewController<UIPickerViewDelegate,UIPickerViewDataSource> @property(strong,nonatomic) UIPickerView *picker; @property(strong,nonatomic) NSMutableArray *sheng; @property(strong,nonatomic) NSMutableArray *shi; @property(strong,nonatomic) NSArray *array; @end
#import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; self.sheng=[NSMutableArray array]; self.shi=[NSMutableArray array]; NSBundle *bundle=[NSBundle mainBundle]; NSString *path=[bundle pathForResource:@"city.plist" ofType:nil]; self.array=[NSArray arrayWithContentsOfFile:path]; for (NSDictionary *ar1 in self.array) { NSArray *ar2=[ar1 objectForKey:@"Cities"]; [self.sheng addObject:[ar1 objectForKey:@"State"]]; for (NSDictionary *ar3 in ar2) { // NSLog(@"%@",[ar3 objectForKey:@"city"]); [self.shi addObject:[ar3 objectForKey:@"city"]]; } } self.picker=[[UIPickerView alloc]initWithFrame:CGRectMake(0, 200, 414, 300)]; self.picker.backgroundColor=[UIColor greenColor]; [self.view addSubview: self.picker]; self.picker.delegate=self; self.picker.dataSource=self; } #pragma mark -数据源 nubertOfComponentsInPickerView: -(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView { return 2; } #pragma mark - 数据源 pickerView: attributedTitleForRow: forComponent: -(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{ if (component==0) { return self.sheng.count; } return self.shi.count; } #pragma mark - 显示信息方法 delegate -(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component { if (component==0) { return self.sheng[row]; } return self.shi[row]; } #pragma mark -选中行的信息 -(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component { if (component==0) { //清除上次选择地城市 [self.shi removeAllObjects]; NSDictionary *dic=[self.array objectAtIndex:row]; // NSLog(@"%@",dic); NSArray *arr1=dic[@"Cities"]; NSMutableArray * arr2 =[NSMutableArray array]; for (NSDictionary * dic in arr1) { [arr2 addObject:dic[@"city"]]; } self.shi=arr2; [self.picker selectRow:row inComponent:0 animated:YES]; [self.picker reloadComponent:1]; } else { NSInteger firstRow=[self.picker selectedRowInComponent: 0]; NSInteger secondRow=[self.picker selectedRowInComponent:1]; NSString *firstString=[self.sheng objectAtIndex:firstRow]; NSString *secondString=[self.shi objectAtIndex:secondRow]; NSString *message=[NSString stringWithFormat:@"您确定要选择%@%@吗?",firstString,secondString]; UIAlertController *alertMessage=[UIAlertController alertControllerWithTitle:@"提示" message:message preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *cancle=[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]; UIAlertAction *ok=[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil]; [alertMessage addAction:cancle]; [alertMessage addAction:ok]; [self presentViewController:alertMessage animated:YES completion:nil]; NSLog(@"%@%@",firstString,secondString); } } #pragma mark - 行高 -(CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component { if (component==0) { return 80; } return 50; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
相关文章推荐
- UIScrollView的简单使用
- Sublim 3中设置新的builid system
- 关于UIButton的一些用法
- leetcode 63. Unique Paths II | Java最短代码实现
- 2015 UESTC Training for Data Structures 秋实大哥下棋
- LeetCode-187. Repeated DNA Sequences
- 快速排序--QuickSort
- 26.手势识别之UILongPressGestureRecognizer
- 2016太原UI设计师讲通道的原理,修图必学
- Collection子接口(List/Set/Queue/SortedSet)
- PAT (Advanced Level) Practise 1051 Pop Sequence (25)
- 用编程的方式定义UI界面
- Angular-ui-router路由,View管理
- iOS基础UI瀑布流界面简单搭建
- ios基于UICollectionView的 横向瀑布流
- quicksort的实现
- Codeforces Beta Round #3 D. Least Cost Bracket Sequence 优先队列
- nfs客户端UID/GID映射为nobody:nobody的解决办法
- celue设计模式
- css3 ani-2-trans, any to transform!!! css3 animation jingsui! Hover