为什么代码 return 清空选择器视图

Why does the code return empty picker view

我正在尝试在选取器视图中构建一个包含两个相关组件的屏幕。我将 picker 与 属性、dataSource 和 delegate 连接到场景顶部的视图控制器的黄色按钮,我还将按钮操作与 buttonPressed 方法连接。

构建后,我收到没有数据的空选择器的白屏。为什么会这样?

     #import "BIDDependentComponentPickerViewController.h"

     #define kStateComponent 0
     #define kZipComponent   1

     @interface BIDDependentComponentPickerViewController ()

     @property (strong, nonatomic) NSDictionary *stateZips;
     @property (strong, nonatomic) NSArray *states;
     @property (strong, nonatomic) NSArray *zips;
     @property (weak, nonatomic) IBOutlet UIPickerView *dependentPicker;


     @end

     @implementation BIDDependentComponentPickerViewController

     - (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 *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.
      }


       - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender     
       {

       }

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

      }


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

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

    @end

确保您已经在包中添加了 statedictionary 文件。
您也可以记录 array/dictionary 以检查值是否存在。