【发布时间】:2014-03-11 16:38:21
【问题描述】:
我有一个 UIPickerView 被“推送”到 UINavigationController,如下所示:
[self.navigationController pushViewController:vc animated:YES];
我想设置选中的行。
我在 ViewDidAppear 中添加了:
for (int i = 0; i < [countryCodes count]; i++)
{
if ([[countryCodes objectAtIndex:i] isEqualToString:selectedCountryCode]){
[_countryPicker selectRow:i inComponent:0 animated:YES];
countrySelectedRow = i;
break;
}
}
[_countryPicker reloadAllComponents];
其中 i 是动态的(根据该视图控制器中正在更改的数据进行更改)
只有在我重新启动应用程序时才有效。
如果我在导航中来回切换它不起作用
如何让 UIPickerView 选择正确的行?
我可以在调试模式下看到 viewDidAppear 中的行被调用。也许组件正在创建,我无法更改它?
这就是我创建 UIPickerView 的方式:
- (void)viewDidLoad
{
_countryPicker = [[UIPickerView alloc] init];
[self initPicker:_countryPicker textField:_countryText];
}
- (void)initPicker:(UIPickerView*)pickerView textField:(UITextField*) textField
{
CGRect pickerFrame = CGRectMake(0, 0, 200, 216);
pickerView.frame = pickerFrame;
pickerView.userInteractionEnabled = YES;
pickerView.dataSource = self;
pickerView.hidden = YES;
pickerView.delegate = self;
pickerView.showsSelectionIndicator = YES;
[self.view addSubview:pickerView];
[textField setInputView:pickerView];
textField.delegate = self;
[pickerView removeFromSuperview];
}
【问题讨论】:
-
你能显示设置
i的代码吗 -
完成我编辑了我的代码
-
你是否设置了一个断点来查看 isEqual 是否发生过?
-
只是好奇:为什么要添加到 self.view 然后删除?
-
使用您的所有代码构建一个模拟,如图所示,进入 subVC 并返回,进入父级并返回(重新创建选择器)没有问题,并且将选择器设置为一行都可以正常工作。 (顺便说一句,你的第一句话不正确,不是吗?,你不是在推动pickerview,你是在推动一个视图控制器,其中包含一个以pickview 作为输入视图的textview。)你是否用
NSLog(@"picker was(is): %d",[_countryPicker selectedRowInComponent:0]);包围了setComponent绝对确定它正在设置/检索 selectedRow
标签: ios iphone objective-c uinavigationcontroller uipickerview