iOS.UIKit.11.UIPickerView

一、案例介绍:点击UIButton,将选中的省市信息显示在UILabel中,如图01,02

图01图02

二、案例步骤:

1、选择Simple View Aplication,取名cq.37.普通选择器,如图03

图03

2、Main.storyboard,如图04

3、provinces_cities.plist

4、CQ37ViewController.h


#import <UIKit/UIKit.h>

@interface CQ37ViewController : UIViewController<UIPickerViewDataSource,UIPickerViewDelegate>

@property (weak,nonatomic) IBOutlet UIPickerView *pickerView;

@property (weak,nonatomic) IBOutlet UILabel *label;

@property (strong,nonatomic) NSDictionary *pickerData;
@property (strong,nonatomic) NSArray *pickerProvincesData;
@property (strong,nonatomic) NSArray *pickerCitiesData;

- (IBAction)onclick:(id)sender;
@end

5、CQ37ViewController.m


#import "CQ37ViewController.h"

@interface CQ37ViewController ()

@end

@implementation CQ37ViewController

- (void)viewDidLoad
{
[super viewDidLoad];

NSBundle *mainBundle = [NSBundle mainBundle];
NSString *plistPath = [mainBundle pathForResource:@"provinces_cities" ofType:@"plist"];

NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:plistPath];
self.pickerData = dict;

self.pickerProvincesData = [dict allKeys];

NSString *selectedProvince = [self.pickerProvincesData objectAtIndex:0];
self.pickerCitiesData = [self.pickerData objectForKey:selectedProvince];

self.pickerView.delegate = self;
self.pickerView.dataSource = self;
}

- (IBAction)onclick:(id)sender
{
NSInteger row1 = [self.pickerView selectedRowInComponent:0];
NSInteger row2 = [self.pickerView selectedRowInComponent:1];

NSString *selected1 = [self.pickerProvincesData objectAtIndex:row1];
NSString *selected2 = [self.pickerCitiesData objectAtIndex:row2];

NSString *title = [[NSString alloc] initWithFormat:@"%@,%@",selected1,selected2];

self.label.text = title;
}
#pragma mark UIPickerViewDataSource method
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 2;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
if (component == 0) {
return [self.pickerProvincesData count];
}else{
return [self.pickerCitiesData count];
}
}
#pragma mark 实现协议UIPickerViewDelegate方法
-(NSString *)pickerView:(UIPickerView *)pickerView
titleForRow:(NSInteger)row forComponent:(NSInteger)component {
if (component == 0) {//选择省份名
return [self.pickerProvincesData objectAtIndex:row];
} else {//选择市名
return [self.pickerCitiesData objectAtIndex:row];
}
}

- (void)pickerView:(UIPickerView *)pickerView
didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
if (component == 0) {
NSString *seletedProvince = [self.pickerProvincesData objectAtIndex:row];
NSArray *array = [self.pickerData objectForKey:seletedProvince];
self.pickerCitiesData = array;
[self.pickerView reloadComponent:1];
}
}
@end

iOS.UIKit.11.UIPickerView,布布扣,bubuko.com

时间: 2024-12-20 12:09:34

iOS.UIKit.11.UIPickerView的相关文章

iOS UIKit:viewController之动画(5)

当弹出一个view controller时,UIKit提供了一些标准转换动画,并且也支持用户自定义的动画效果. 1 UIView动画 UIView是自带动画实现功能,其中有两种方式实现:        1) animateWithDuration系列方法        2) transitionFromViewController方法 1.1 animateWithDuration 只要在该方法的animations block中修改UIView的动画属性,那么即可实现动画变换,所以为conta

iOS UIKit 框架 346 篇文档分类整理 - 预告

太阳火神的美丽人生 (http://blog.csdn.net/opengl_es) 本文遵循"署名-非商业用途-保持一致"创作公用协议 转载请保留此句:太阳火神的美丽人生 -  本博客专注于 敏捷开发及移动和物联设备研究:iOS.Android.Html5.Arduino.pcDuino,否则,出自本博客的文章拒绝转载或再转载,谢谢合作. 当前正在进行的是 "iOS Foundation 框架 224 篇相关文档分类整理",量很大,但会根据实际开发中的使用频繁程序

iOS.UIKit.12.UICollectionView

一.案例介绍:以集合形式展现图片,如图01 图01 二.案例步骤: 1.选择Simple View Aplication,取名cq.38.集合视图,如图02 图02 2.Main.storyboard,如图03 图03 3.events.plist,如图04,05 4.CQ38ViewController.h #import <UIKit/UIKit.h> @interface CQ38ViewController : UICollectionViewController @property

iOS.UIKit.09.UINavigationBar

一.点击UINavigationBar中的UIBarButtonItem,改变UILabel中的文本,如图01,02,03 图01图02图03 二.项目步骤: 1.选择Simple View Aplication,取名cq.40.UINavigationBar,如图04 图04 2.Main.storyboard,如图05 3.CQ40ViewController.h #import <UIKit/UIKit.h> @interface CQ40ViewController : UIViewC

iOS.UIKit.08.UIToolbar

一.案例介绍:点击UIToolbar中的UIBarButtonItem,改变UILabel中的文本,如图01,02,03 图01图02图03 二.案例步骤: 1.选择Simple View Aplication,取名cq.33.工具栏和导航栏,如图04 2.Maim.storyboard,如图05 3.CQ33ViewController.h #import <UIKit/UIKit.h> @interface CQ33ViewController : UIViewController @pr

iOS.UIKit.10.UIDatePicker

一.案例介绍:点击屏幕中的UIButton,将在UIDatePicker选择的日期显示在UILabel中,如图01,02 图01图02 二.案例步骤: 1.选择Simple View Aplication,取名cq.36.日期选择器,如图03 图03 2.Main.storyboard,如图04 3.CQ36ViewController.h #import <UIKit/UIKit.h> @interface CQ36ViewController : UIViewController @pro

iOS.UIKit.07.UIAlertView_UIActionSheet

一.案例介绍:点击第一个按钮弹出提示框:点击第二个按钮弹出操作表.如图01,图02,图03 图01图02图03 二.案例步骤: 1.选择Simple View Aplication,取名cq.32.警告框和操作表,如图04 2.Main.storyboard 3.CQ32ViewController.h #import <UIKit/UIKit.h> @interface CQ32ViewController : UIViewController<UIAlertViewDelegate,

iOS.UIKit.06.UIProgressView_UIActivityIndicatorView

一.案例介绍:点击按钮upload,活动指示器转动,再次点击停止转动:点击download按钮进度条加载满后提示,如图01,图02. 图01图02 二.案例步骤: 1.选择Simple View Aplication,取名cq.31.活动指示器和进度条,如图03. 图03 2.Main.storyboard,如图04,图05. 图04图05 3.CQ31ViewController.h #import <UIKit/UIKit.h> @interface CQ31ViewController

iOS.UIKit.05.UIScrollView

一.案例介绍:利用UIScrollView, 避免键盘遮挡控件:图01.图02其实是存在第三个UIButton的,滑动屏幕可以看到,如图06.效果如图01,图02. 图01图02图03图06 二.案例步骤: 1.选择Single View Application新建项目,取名cq.30.ScrollView,如图03. 2.Main.storyboard如图04,图05. 图04图05 3.CQ30ViewController.h #import <UIKit/UIKit.h> @interf