如何将plist中的数据拉取到UIPickerView?

发布于 2024-10-31 09:08:57 字数 49 浏览 2 评论 0原文

能否帮忙提供一下如何将数据从 plist 提取到 UIPickerView 的教程?

can please help to provide the tutorial on how to pull the data from plist to UIPickerView?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

半城柳色半声笛 2024-11-07 09:08:57

这对你有用。

 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];
    }
}


@end

it useful for u.

 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];
    }
}


@end
硪扪都還晓 2024-11-07 09:08:57

您可以使用 CNSivakumr 的代码创建这样的 plist!

[plistfilename.plist][1]

[1]:https://i .sstatic.net/zVrSe.png/Users/satishmishra/Desktop/Scr​​een 拍摄于 2013 年 4 月 3 日下午 12 月 27 日.png

You can create your plist like this with CNSivakumr's code!

[plistfilename.plist][1]

[1]: https://i.sstatic.net/zVrSe.png/Users/satishmishra/Desktop/Screen Shot 2013-04-03 at 12.27.24 PM.png

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文