picker控件详解与使用,(实现省市的二级联动)
picker控件详解与使用,(实现省市的二级联动)
第一步:新建一个单视图(single view)的工程,

命名为pickerTest,不要勾选下面两个选项,第一个是新版本里面的,第二个是单元测试,现在用不着。

点击next ->creat之后,打开工具栏:

在下面的控件工具栏中往视图上拖放一个Picker View控件,一个UIButton控件,一个UILable控件,(所有在前端可以看得见的控件都继承自UIView)

修改UIButton的title属性,设置为click 然后点击分栏按钮,为控件连线(这种方式相对于手动去写,要快速很多)。
依次为UILable 和 Picker View 控件添加插座变量,并且为UIButton添加Action
右击picker控件,将 Datesource和delegate连线至File's Owner, 设置它的数据源和代理。
在 cidpViewController.h文件中添加几个变量和遵循 UIPickerViewDelegate 协议,这样就可以用UIPickerViewDelegate里面的几个方法了 。
@interface cidpViewController : UIViewController<UIPickerViewDelegate>

#import <UIKit/UIKit.h>
@interface cidpViewController : UIViewController<UIPickerViewDelegate> {
UILabel *lblTitle;
UIPickerView *picker;
NSString* strProvince;
NSString* strCity;
NSMutableArray* aProvince;
NSMutableArray* aCity;
NSArray* tempArray;
}
@property (strong, nonatomic) IBOutlet UIPickerView *picker;
@property (strong, nonatomic) IBOutlet UILabel *lblTitle;
- (IBAction)btnClick:(id)sender;
@end


#import "cidpViewController.h" @implementation cidpViewController @synthesize picker;
@synthesize lblTitle; - (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
} #pragma mark - View lifecycle - (void)viewDidLoad
{
[super viewDidLoad];
//初始哈5个省份
aProvince = [[NSMutableArray alloc] initWithObjects:@"北京",@"甘肃",@"山西",@"湖北",@"广州", nil];
//初始化5个数组 ,分别初始化为5个省的城市
NSArray* array1 = [NSArray arrayWithObjects:@"海淀",@"昌平",@"朝阳",@"西城",@"丰台", nil];
NSArray* array2 = [NSArray arrayWithObjects:@"兰州",@"白银",@"张掖",@"陇西",@"天水", nil];
NSArray* array3 = [NSArray arrayWithObjects:@"太原",@"大同",@"运城",@"晋城",@"五台山", nil];
NSArray* array4 = [NSArray arrayWithObjects:@"武汉",@"荆州",@"襄阳",@"赣州", nil];
NSArray* array5 = [NSArray arrayWithObjects:@"广州",@"佛山",@"尖沙咀",@"中山", nil];
aCity = [[NSMutableArray alloc] initWithObjects:array1,array2,array3,array4,array5, nil];
//tempArray 用来初始化第二个 ,并且引用一次,避免提前释放,内存出错
tempArray = [array1 retain];
// 用来第一次显示UIlable,负责的话就会显示 null,null
strProvince = [aProvince objectAtIndex:0];
strCity = [tempArray objectAtIndex:0];
[aProvince release];
[aCity release]; }
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView*)pickerView{
//返回2 表示将有两个component(滚轮)
return 2;
} -(NSInteger)pickerView:(UIPickerView*)pivkerView numberOfRowsInComponent:(NSInteger)component{
if(component == 0){
// 返回省份数组的长度
return [aProvince count];
}else{
//返回省份对应城市数组的长度。
[tempArray count];
} - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
if(component == 0){
//返回对应row 的数组元素。
return [aProvince objectAtIndex:row];
}else{ return [tempArray objectAtIndex:row];
}
} - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
if(component == 0){
//返回省份对应的城市数组,
tempArray = [aCity objectAtIndex:row]
[picker selectRow:0 inComponent:1 animated:YES];
[picker reloadComponent:1];
strProvience = [aProvince objectAtIndex:row];
strCity = [tempArray objectAtIndex:0];
}else{
//城市滚轮滚动时,strCity也随着改变。
strCity = [tempArray objectAtIndex:0];
}
lblTitle.text = [[NSString alloc] initWithFormat:@"%@,%@",strProvince,strCity,nil];
} - (void)viewDidUnload
{
[self setLblTitle:nil];
[self setPicker:nil];
[super viewDidUnload];
} - (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
} - (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
} - (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
} - (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
} - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
} - (IBAction)btnClick:(id)sender {
lblTitle.text = [[NSString alloc] initWithFormat:@"%@,%@",strProvince,strCity,nil];
}
-(void)dealloc{
[aProvince release];
[aCity release];
[lblTitle release];
[strCity release];
[strProvince release];
[picker release];
[super dealloc]; }
@end


@protocol UIPickerViewDataSource<NSObject>
@required // returns the number of 'columns' to display.
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView; // returns the # of rows in each component..
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component;
@end

UIPickerViewDelegate协议中的五个方法,非必须实现,

@protocol UIPickerViewDelegate<NSObject>
@optional // returns width of column and height of row for each component.
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component;
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component; // these methods return either a plain UIString, or a view (e.g UILabel) to display the row for the component.
// for the view versions, we cache any hidden and thus unused views and pass them back for reuse.
// If you return back a different object, the old one will be released. the view will be centered in the row rect
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component;
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view; - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component; @end

picker控件详解与使用,(实现省市的二级联动)的更多相关文章
- Objective-C ,ios,iphone开发基础:picker控件详解与使用,(实现省市的二级联动)
第一步:新建一个单视图(single view)的工程, 命名为pickerTest,不要勾选下面两个选项,第一个是新版本里面的,第二个是单元测试,现在用不着. 点击next ->creat之 ...
- IOS—UITextFiled控件详解
IOS—UITextFiled控件详解 //初始化textfield并设置位置及大小 UITextField *text = [[UITextField alloc]initWithFrame:CGR ...
- Switch控件详解
Switch控件详解 原生效果 5.x 4.x 布局 <Switch android:id="@+id/setting_switch" android:layout_widt ...
- ToolBar控件详解
ToolBar控件详解 在Activity中添加ToolBar 1.添加库 dependencies { ... compile "com.android.support:appcompat ...
- Spinner控件详解
Spinner控件详解 效果图 修改Spinner样式 在介绍之前,先看一下系统原生的样式 6.x & 5.x系统样式 4.x系统样式 官方文档 XML属性 方法 描述 android:dro ...
- Android开发:文本控件详解——TextView(一)基本属性
一.简单实例: 新建的Android项目初始自带的Hello World!其实就是一个TextView. 在activity_main.xml中可以新建TextView,从左侧组件里拖拽到右侧预览界面 ...
- Android开发:文本控件详解——TextView(二)文字跑马灯效果实现
一.需要使用的属性: 1.android:ellipsize 作用:若文字过长,控制该控件如何显示. 对于同样的文字“Android开发:文本控件详解——TextView(二)文字跑马灯效果实现”,不 ...
- C++ CComboBox控件详解
转载:http://blog.sina.com.cn/s/blog_46d93f190100m395.html C++ CComboBox控件详解 (2010-09-14 14:03:44) 转载▼ ...
- 【iOS 开发】基本 UI 控件详解 (UIButton | UITextField | UITextView | UISwitch)
博客地址 : http://blog.csdn.net/shulianghan/article/details/50051499 ; 一. UI 控件简介 1. UI 控件分类 UI 控件分类 : 活 ...
随机推荐
- TS流文件
简单介绍编辑 随着从HDTV录制的高清节目在网上的流传,烧友们对TS这个名词大概已经不陌生了.但随之而来就是怎样播放.怎样加入字幕等等的一系列问题.本文将重点介绍一下这方面的应用操作. 先来简要介绍一 ...
- js调用wcf 的SOA
jquery 调用wcf 的SOA架构,将三层架构运用到SOA的架构中来 经过前面3天的学习,我想大家应该对SOA的架构有了初步的了解,其实 SOA与三层架构并不冲突,而是三层架构的升级版. 来看下传 ...
- json 解析解乱码
1. 该法的字符编码: 串店txt文档文档都有自己的编码,例如utf-8,ansi等待,但当 存款txt文件.其编码将和txt编码文件本身一致. 例如,之前的字符编码ansi.txt该文件的编码是u ...
- MyReport报表引擎2.7.6.7新功能
新增二维码控件PDF417 设计器新增数据选项卡,可以拖放字段进行绑定 相关链接 MyReport演示.产品站点 相关文章 MyReport专栏
- MVC 5 Web编程2 -- URL映射
ASP.NET MVC 5 Web编程2 -- URL映射(路由原理) 2015-02-12 08:50 by hangwei, 704 阅读, 5 评论, 收藏, 编辑 本章将讲述ASP.NET M ...
- tomcat加载类的顺序
/bin:存放启动和关闭tomcat的脚本文件: /conf:存放tomcat的各种配置文件,比如:server.xml /server/lib:存放tomcat服务器所需要的各种jar文件(jar文 ...
- c语言中实现从0-1的随机数输出
原文:c语言中实现从0-1的随机数输出 今天晚上同学问了一个巨简单的问题,问我怎么用c语言输出0-1的随机数,可别说,一时之间还想不出来.在写的过程中发现,直接调用random函数还不能实现,用以下方 ...
- JUnit介绍
8.1.1 JUnit简介 JUnit主要用来帮助开发人员进行Java的单元测试,其设计非常小巧,但功能却非常强 大. 下面是JUnit一些特性的总结: — 提供的API可以让开发人员写出测试结果明 ...
- sql注入工具sqlmap使用参数说明
Options(选项):--version 显示程序的版本号并退出-h, --help 显示此帮助消息并退出-v VERBOSE 详细级别:0-6(默认为1)Target(目标):以下至少需要设置其中 ...
- bat启动/停止oracle服务
原文:bat启动/停止oracle服务 自己的电脑比较慢,尤其装了oracle10g后,服务开启和关闭用bat文件操作省事点 开启服务 @echo offnet start OracleService ...