创建关联的UIPickerView 显示省市
2011-04-15 17:18
148 查看
创建关联的UIPickerView 显示省市
iPhone2010-03-28 12:42:57
阅读225
评论2
字号:大
中
小
订阅
//
// DependentComponentPickerViewController.m
// Pickers
//
// Created by Jeff LaMarche on 7/7/08.
// Copyright 2008 __MyCompanyName__. All rights reserved.
//
#import
"DependentComponentPickerViewController.h"
@implementation
DependentComponentPickerViewController
@synthesize picker;
@synthesize
stateZips;
@synthesize states;
@synthesize zips;
- (IBAction
)butonPressed:(id
)sender
{
NSInteger
stateRow = [
picker
selectedRowInComponent:
kStateComponent
];
NSInteger
zipRow = [
picker
selectedRowInComponent:
kZipComponent
];
NSString
*state = [self
.states
objectAtIndex
:stateRow];
NSString
*zip = [self
.zips
objectAtIndex
:zipRow];
NSString
*title = [[
NSString
alloc
]
initWithFormat
:
@"You selected zip code %@.", zip];
NSString
*message = [[NSString
alloc
] initWithFormat
:@"%@ is in %@"
, zip, state
];
UIAlertView
*alert = [[
UIAlertView
alloc]
initWithTitle:title
message:message
delegate:
nil
cancelButtonTitle:
@"OK"
otherButtonTitles:
nil
];
[alert show
];
[alert release
];
[title release
];
[message release
];
}
- (id
)initWithNibName:(NSString
*)nibNameOrNil bundle:(NSBundle
*)nibBundleOrNil {
if
(self
= [super
initWithNibName
:nibNameOrNil bundle
:nibBundleOrNil]) {
// Initialization code
}
return
self;
}
- (void
)viewDidLoad {
NSBundle
*bundle = [NSBundle
mainBundle
];
NSString
*plistPath = [bundle pathForResource
:@"statedictionary"
ofType
:@"plist"
];
NSDictionary
*dictionary = [[NSDictionary
alloc
] initWithContentsOfFile
:plistPath];
self
.stateZips
= dictionary;
[dictionary release
];
NSArray
*components = [self
.stateZips
allKeys
];
NSArray
*sorted = [components
sortedArrayUsingSelector:
@selector
(
compare:)];
self
.states
= sorted;
NSString
*selectedState = [self
.states
objectAtIndex
:0
];
NSArray
*array = [stateZips
objectForKey
:selectedState];
self
.zips
= array;
}
- (BOOL
)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation
)interfaceOrientation {
// Return YES for supported orientations
return
(interfaceOrientation ==
UIInterfaceOrientationPortrait);
}
- (void
)didReceiveMemoryWarning {
[
super
didReceiveMemoryWarning
];
// Releases the view if it doesn't have a superview
// Release anything that's not essential, such as cached data
}
- (void
)dealloc {
[
picker
release];
[
stateZips
release
];
[
states
release];
[
zips
release];
[
super
dealloc];
}
#pragma mark -
#pragma mark Picker Data Source Methods
- (NSInteger
)numberOfComponentsInPickerView:(UIPickerView
*)pickerView
{
return
2
;
}
- (NSInteger
)pickerView:(UIPickerView
*)pickerView numberOfRowsInComponent:(NSInteger
)component
{
if
(component == kStateComponent
)
return [
self.
states
count
];
return [
self.
zips
count
];
}
#pragma mark Picker Delegate Methods
- (NSString
*)pickerView:(UIPickerView
*)pickerView titleForRow:(NSInteger
)row forComponent:(NSInteger
)component
{
if
(component == kStateComponent
)
return
[
self
.
states
objectAtIndex:row];
return
[
self
.
zips
objectAtIndex:row];
}
- (void
)pickerView:(UIPickerView
*)pickerView didSelectRow:(NSInteger
)row inComponent:(NSInteger
)component
{
if
(component == kStateComponent
)
{
NSString
*selectedState = [self
.states
objectAtIndex
:row];
NSArray
*array = [stateZips
objectForKey
:selectedState];
self
.zips
= array;
[picker
selectRow:0
inComponent:kZipComponent
animated:YES
];
[
picker
reloadComponent:
kZipComponent
];
}
}
- (CGFloat
)pickerView:(UIPickerView
*)pickerView widthForComponent:(NSInteger
)component
{
if
(component == kZipComponent
)
return
90
;
return
205
;
}
@end
相关文章推荐
- 创建关联的UIPickerView 显示省市
- iOS UIPickerView 显示全国省市
- iOS学习之UIPickerView控件的关联选择
- UIPickerView选择省市
- iOS之UIPickerView的使用--多列关联选择器
- UIPickerView控件中自定义显示的字体大小及样式
- 如何自定义UIPickerView中文本的大小和文本靠左或靠右显示?
- iOS学习之UIPickerView控件的关联选择
- UIPickerView的属性和常用方法 举例:显示省份和城市
- UIPickerView在UIPopoverController中正确显示方法
- UIPickerView【随机点菜系统 && 省市联动 && 选择国旗】
- iOS学习之UIPickerView控件的关联选择
- [ios开发技巧之]UIPickerView的数据循环显示
- iOS学习之UIPickerView控件的关联选择
- iOS学习之UIPickerView控件的关联选择
- iOS学习之UIPickerView控件的关联选择
- IOS开发基础UIPickerView循环显示
- UIPickerView 显示与隐藏
- iOS省市区三级联动UIPickerView(含省市区id)
- UIPickerView在UIPopoverController中正确显示方法