在iOS 6 UIPickerView selectrow崩溃

我的应用程序完美地构build和运行在iOS 5.x中,但是当我调用iOS 6中的UIPickerView selectRow:inComponent:animated:方法时,它会崩溃。

代码:

 [_pickerview selectRow:1 inComponent:0 animated:NO]; 

我知道这种方法是不是在iOS6的工作,当我GOOGLE了,但我想知道其他方法来做到这一点?

你的崩溃日志说你已经在代码中使用-1 ,其中应该是范围{0,1}中的一个数字。 但是在你粘贴的代码中确实使用了1 。 所以你需要检查你的参数为你的xxxyyy

 [_pickerview selectRow:xxx inComponent:yyy animated:NO]; 

上面的答案是正确的,但是如果数组的值与select器值不同,那么就会出现这个错误。

这里是我的代码,我这样初始化: amPmArray = [[NSMutableArray alloc] initWithObjects:@"AM",@"PM", nil];

并在[picker selectRow:[amPmArray indexOfObject:currentTimeAMPMString] inComponent:5 animated:NO];设置值: [picker selectRow:[amPmArray indexOfObject:currentTimeAMPMString] inComponent:5 animated:NO];

而且我总是在currentTimeAMPMString中获得像am,pm这样的小写值,而且我的数组不包含这种types的项目,所以这就是为什么我会崩溃的错误。

经过3天的发现,我发现如果您使用select器与textField关联。 PLZ不需要将任何文本分配给textField。 当你使用下面的文本时,这意味着分配的testing应该存在于数组中。

[self> pickerView selectRow:[self> dataArray indexOfObject:self> textField.text] inComponent:0 animated:NO];

使用这个。

[self> pickerView selectRow:[self> dataArray objectAtIndex:row] inComponent:0 animated:NO];