注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

熊猫正正的博客

熊猫正正的天空

 
 
 

日志

 
 

实现一个相互依懒的PickerView  

2013-04-28 21:32:26|  分类: ios开发 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

今天实现了一个相互依懒的PickerView控制,代码如下:

#define kStateComponent 0

#define kZipComponent   1


#import <UIKit/UIKit.h>


@interface BIDDependentComponentPickerViewController : UIViewController <UIPickerViewDelegate,UIPickerViewDataSource>


@property (strong, nonatomic) IBOutlet UIPickerView *dependentPicker;

@property (strong, nonatomic) NSDictionary *stateZips;

@property (strong, nonatomic) NSArray *states;

@property (strong, nonatomic) NSArray *zips;


- (IBAction) buttonPressed;


@end


-------------------------------


#import "BIDDependentComponentPickerViewController.h"


@interface BIDDependentComponentPickerViewController ()


@end


@implementation BIDDependentComponentPickerViewController


- (IBAction) buttonPressed

{

    NSInteger stateRow = [self.dependentPicker selectedRowInComponent:kStateComponent];

    NSInteger zipRow = [self.dependentPicker selectedRowInComponent:kZipComponent];

    

    NSString *state = self.states[stateRow];

    NSString *zip = self.zips[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, nil];

    [alert show];

}


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{

    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if (self) {

        // Custom initialization

    }

    return self;

}


- (void)viewDidLoad

{

    [super viewDidLoad];

    // Do any additional setup after loading the view from its nib.

    NSBundle *bundle = [NSBundle mainBundle];

    NSURL *plistURL = [bundle URLForResource:@"statedictionary" withExtension:@"plist"];

    self.stateZips = [NSDictionary dictionaryWithContentsOfURL:plistURL];

    NSArray *allStates = [self.stateZips allKeys];

    NSArray *sortedStates = [allStates sortedArrayUsingSelector:@selector(compare:)];

    self.states = sortedStates;

    NSString *selectedState = self.states[0];

    self.zips = self.stateZips[selectedState];

}


- (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


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

    }

    else

    {

        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[row];

    }

    else

    {

        return self.zips[row];

    }

}


- (void) pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component

{

    if (component == kStateComponent)

    {

        NSString *selectedState = self.states[row];

        self.zips = self.stateZips[selectedState];

        [self.dependentPicker reloadComponent:kZipComponent];

        [self.dependentPicker selectRow:0

                            inComponent:kZipComponent

                               animated:YES];

    }

}


- (CGFloat) pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component

{

    if (component == kZipComponent)

    {

        return 90;

    }

    else

    {

        return 200;

    }

}


@end


  评论这张
 
阅读(39)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017