我有一個正在加載到UIPickerView的plist。我可以訪問數組信息並將其加載到其中一個組件中的選取器中。我想要做的是訪問項目0或項目1,並根據條件將它們顯示在UILabel上。UIPicker訪問來自plist的項目
我不知道如何訪問項目0或項目1(字符串值)。關於如何去做這件事的任何提示?謝謝您的幫助。
這裏是一個圖像,以澄清什麼我談論:
我有一個正在加載到UIPickerView的plist。我可以訪問數組信息並將其加載到其中一個組件中的選取器中。我想要做的是訪問項目0或項目1,並根據條件將它們顯示在UILabel上。UIPicker訪問來自plist的項目
我不知道如何訪問項目0或項目1(字符串值)。關於如何去做這件事的任何提示?謝謝您的幫助。
這裏是一個圖像,以澄清什麼我談論:
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
感謝您的帖子。如何訪問plist中的Item 0或Item 1(字符串值)。說如果我想獲得ObjectAtIndex的字符串值:0和項目1的字符串值。謝謝 – hanumanDev
如果你得到完整的想法,然後看到這個鏈接:-http://stackoverflow.com/questions/5598183/how-to -pull最數據從-plist中到uipickerview – ram
我需要訪問項目0或基於條件從字典中的項目1。 – hanumanDev