gpt4 book ai didi

objective-c - UIPicker 从 plist 访问项目

转载 作者:行者123 更新时间:2023-11-29 05:01:15 25 4
gpt4 key购买 nike

我有一个正在加载到 UIPickerView 中的 plist。我可以访问数组信息并将其加载到组件之一的选择器中。我想做的是访问 Item 0 或 Item 1 并根据条件将它们显示在 UILabel 上。

我不知道如何访问 Item 0 或 Item 1(字符串值)。关于我如何去做这件事有什么建议吗?感谢您的帮助。

这是一张图片来澄清我在说什么:

enter image description here

最佳答案

pickerViewcontroller.h

#import <UIKit/UIKit.h>
#define kStateComponent 0
#define kZipComponent 1

@interface PickerViewController : UIViewController
<UIPickerViewDataSource,UIPickerViewDelegate>{
IBOutlet UIPickerView *dpicker;
NSDictionary *stateZip;
NSArray *states;
NSArray *zips;

}
@property (nonatomic,retain) UIPickerView *dpicker;
@property (nonatomic,retain) NSDictionary *stateZip;
@property (nonatomic, retain) NSArray *states;
@property (nonatomic, retain) NSArray *zips;
@end

pickerViewcontroller.m

#import "PickerViewController.h"


@implementation PickerViewController
@synthesize dpicker;
@synthesize stateZip;
@synthesize states;
@synthesize zips;


-(void) viewDidLoad{

NSBundle *bundle = [NSBundle mainBundle];

NSString *plistPath =[bundle pathForResource:@"plistfilename" ofType:@"plist"];

NSDictionary *dictionary = [[NSDictionary alloc] initWithContentsOfFile:plistPath];
self.stateZip=dictionary;
[dictionary release];


NSArray *component = [self.stateZip allKeys];
NSArray *sorted =[component sortedArrayUsingSelector:@selector(compare:)];
self.states=sorted;


NSString *selectedState = [self.states objectAtIndex:0];
NSArray *array = [stateZip objectForKey:selectedState];
self.zips = array;


}




#pragma mark -
#pragma mark Memory management

- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];

// Relinquish ownership any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
// Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
// For example: self.myOutlet = nil;
}

- (void)dealloc {
[dpicker release];
[stateZip 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=[stateZip objectForKey:selectedState];
self.zips=array;
[dpicker selectRow:0 inComponent:kZipComponent animated:YES];
[dpicker reloadComponent:kZipComponent];
}
}


@endhere

关于objective-c - UIPicker 从 plist 访问项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6882603/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com