下面是一些效果图

下面是代码。有些枯燥 , 其实并不难 。

#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

UIPickerView简单应用的更多相关文章

  1. iOS开发——高级UI之OC篇&UIdatePicker&UIPickerView简单使用

    UIdatePicker&UIPickerView简单使用 /***************************************************************** ...

  2. UIPickerView 简单操作和实际应用

    1.UIPickerView 选择指示器控件 //选择器的初始化 UIPickerView * pickerView = [[UIPickerView alloc] initWithFrame:CGR ...

  3. iOS学习之UIPickerView控件的简单使用

    UIPickerView控件在给用户选择某些特定的数据时经常使用到,这里演示一个简单的选择数据,显示在UITextField输入框里,把UIPickerView作为输入View,用Toolbar作为选 ...

  4. UIPickerView的简单使用

    UIPickerView是一个选择器它可以生成单列的选择器,也可生成多列的选择器,而且开发者完全可以自定义选择项的外观,因此用法非常灵活,使用也比较简单.下面做了一个关于天气预报的小Demo 用 UI ...

  5. iOS边练边学--UIPickerView和UIDatePicker的简单使用

    一.点菜系统练习(UIPickerView) <1>UIPickerView的常用代理方法介绍 #pragma mark - <UIPickerViewDelegate> // ...

  6. iOS:选择器控件UIPickerView的详解和演示

    选择器控件UIPickerView: 功能:它能够创建一个类似于密码锁式的单列或多列的选择菜单,用户可以通过它设置的代理来选择需要菜单中的任意的数据.例如创建日历.字体表(类型.大小.颜色).图库等. ...

  7. IOS笔记045-UIDatePicker和UIPickerView

    这是两种可以上下滚动的控件. 这是UIDatePicker,可以显示日期和时间. 这个是UIPickerView,显示类似几个选择项的界面. 注意点:PickerView的高度不能改,默认162,Pi ...

  8. iOS开发——UI进阶篇(八)pickerView简单使用,通过storyboard加载控制器,注册界面,通过xib创建控制器,控制器的view创建,导航控制器的基本使用

    一.pickerView简单使用 1.UIPickerViewDataSource 这两个方法必须实现 // 返回有多少列 - (NSInteger)numberOfComponentsInPicke ...

  9. iOS开发UI篇—使用picker View控件完成一个简单的选餐应用

    iOS开发UI篇—使用picker View控件完成一个简单的选餐应用 一.实现效果 说明:点击随机按钮,能够自动选取,下方数据自动刷新. 二.实现思路 1.picker view的有默认高度为162 ...

随机推荐

  1. Auto Mapper03

      经过上一篇博客的学习,大体了解了Auto Mapper的运行机制和操作流程.我们下来学习下,AutoMapper里面具体的一些东西. 一:规则       当我们使用AutoMapper创建实体和 ...

  2. js观察者模式学习

    function Events(){ var obj = {}; this.on=function(key,fn){ var stack; stack = obj[key] || (obj[key] ...

  3. 在后台代码中动态生成pivot项并设置EventTrigger和Action的绑定

    最近在做今日头条WP的过程中,遇到需要动态生成Pivot项的问题.第一个版本是把几个频道写死在xaml里了,事件绑定也写在xaml里,每个频道绑定一个ObservableCollection<A ...

  4. ASP.NET防御XSS跨站攻击

    目前做ASP.NET项目的时候就有遇到过“用户代码未处理HttpRequestValidationException:从客户端***中检测到有潜在危险的 Request.Form/Request.Qu ...

  5. 广义表 Head Tail

    head:取非空广义表的第一个元素 tail:取非空广义表除第一个元素外剩余元素构成的广义表 L=((x,y,z),a,(u,t,w)) head(L)为(x,y,z) head(head(L))为x ...

  6. Tomcat8访问管理页面localhost出现:403 Access Denied

    问题: Access Denied You are not authorized to view this page. If you have already configured the Manag ...

  7. hibernate----component-entity (人-地址-学校)

    package com.ij34.dao; import javax.persistence.*; @Entity @Table(name="school_inf") public ...

  8. 求助,eclipse总是卡在building workspace-CSDN论坛

    1).解决方法 方法1.修改eclipse启动文件 eclipse.ini 中添加启动参数参数: -vmargs -Xmx512m 方法2.关闭自动构建工作区: project -> build ...

  9. java servlet+mysql全过程(原创)

    前段时间写过一篇 servlet+oracle的文章,但是那是因为公司有可能接那么一个项目,然后我当时也比较闲,所以随便学了下,那玩意是白去研究了,因为公司后面并没接到那项目. 这次学servlet用 ...

  10. 历史疑团之EJB

    在学习Sping框架的过程中,看到过很多次关于EJB的批判.使用了SpringMVC但是并没有真性情般体会到它的优点,所以有必要对传统的Java Bean和EJB来做一些了解,无奈百度搜了很多知识,还 ...