gpt4 book ai didi

ios - 为什么代码返回空的选择器 View

转载 作者:行者123 更新时间:2023-11-28 21:47:32 25 4
gpt4 key购买 nike

我正在尝试在选择器 View 中构建一个包含两个相关组件的屏幕。我将选择器与属性、数据源和委托(delegate)连接到场景顶部的 View Controller 的黄色按钮,我还将按钮操作与 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

最佳答案

确保你已经在 bundle 中添加了 statedictionary 文件。
您也可以记录数组/字典以检查值是否存在。

关于ios - 为什么代码返回空的选择器 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29537441/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com