首页 > iOS的PickerView在显示时出现问号

iOS的PickerView在显示时出现问号

1.今天在学习iOS控件的PickerView,按着教程跑一遍代码,结果出现了问题,滚轮里面显示的是问号。如下图:

2.我是根据别人的一个iOS7工程按部就班自己跑了一遍代码,原工程里面没有问题,但是自己跑就是问号。图中下面还有一个对话框,表明数据源应该是没问题了,似乎就是实现滚轮的委托没有成功,就是源码里面,最末尾的那两个方法,好像根本没起作用。

3.调试了很久,仔细检查了DataSource和Delegate的链接,依然无果,现在有可能的原因是,工程使用了一个属性列表plist作为数据来源,

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

这个我是直接复制了那个iOS7工程的,然后add到自己工程里面,不知道是不是这个引起的。望有人能解答:)

下附源码:

#import "KimiDependentComponentPickerViewController.h"

#define kStateComponent 0
#define kZipComponent 1

@interface KimiDependentComponentPickerViewController ()
@property (weak, nonatomic) IBOutlet UIPickerView *dependentPicker;

@property (strong, nonatomic) NSDictionary *stateZips;
@property (strong, nonatomic) NSArray *states;
@property (strong, nonatomic) NSArray *zips;

- (IBAction)buttonPressed:(id)sender;
@end

@implementation KimiDependentComponentPickerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    NSBundle *bundle = [NSBundle mainBundle];
    NSURL *plistURL = [bundle URLForResource:@"statedictionary" withExtension:@"plist"];
    
    self.stateZips = [NSDictionary dictionaryWithContentsOfURL:plistURL];
    NSArray *allstates = [self.stateZips allKeys];
    NSArray *sortStates = [allstates sortedArrayUsingSelector:@selector(compare:)];
    self.states = sortStates;
    
    NSString *selectedState = self.states[0];
    self.zips = self.stateZips[selectedState];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (IBAction)buttonPressed:(id)sender {
    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];
    [alert show];
}

#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 Data 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 selectRow:0 inComponent:kZipComponent animated:YES];
        [self.dependentPicker reloadComponent:kZipComponent];
        
    }
}
@end

titleForRow 里面NSLog("%@", self.states[row]); 这个值。


感觉是那个plist有问题,可以在- (NSString )pickerView:(UIPickerView )pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
里下断点打印一下,看看是不是NSString

【热门文章】
【热门文章】