当我点击按钮时,pickerview
出现,我从那里选择一个城市。如果我再次点击,我想得到我之前选择的内容。
这是我的代码,
-(void)cityButtonPressed{
actionSheet = [[UIActionSheet alloc] initWithTitle:nil
delegate:nil
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:nil];
[actionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];
CGRect pickerFrame = CGRectMake(0, 30, 0, 0);
pickerView = [[UIPickerView alloc] initWithFrame:pickerFrame];
pickerView.showsSelectionIndicator = YES;
NSLog(@"selectedPickerView = %i",selectedPickerView);
[pickerView selectRow:selectedPickerView inComponent:0 animated:NO];// this line is not working
pickerView.dataSource = self;
pickerView.delegate = self;
[actionSheet addSubview:pickerView];
NSString *done = @"Done";
closeButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:done]];
closeButton.momentary = YES;
closeButton.frame = CGRectMake(260, 10, 50.0f, 30.0f);
closeButton.segmentedControlStyle = UISegmentedControlStyleBar;
closeButton.tintColor = [UIColor greenColor];
[closeButton addTarget:self action:@selector(dismissActionSheet:) forControlEvents:UIControlEventValueChanged];
[actionSheet addSubview:closeButton];
[actionSheet showInView:[[UIApplication sharedApplication] keyWindow]];
[actionSheet setBounds:CGRectMake(0, 0, 320, 500)];
[pickerView reloadAllComponents];
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow: (NSInteger)row inComponent:(NSInteger)component {
// Handle the selection
[cityButton setTitle:[cities objectAtIndex:row] forState:UIControlStateNormal];
if (row == 0) {
[cityButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
}else{
[cityButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
}
selectedPickerView = row;
}
我在这里做错了什么。
还有一个问题。当我单击按钮时,出现此错误。
<Error>: CGContextRestoreGState: invalid context 0x0. This is a serious error. This application, or a library it uses, is using an invalid context and is thereby contributing to an overall degradation of system stability and reliability. This notice is a courtesy: please fix this problem. It will become a fatal error in an upcoming update.
我在想,是不是因为这个错误,它不工作了?
最佳答案
您正在创建一个没有标题或按钮的操作表?啊。然后您要添加分段控件作为可点击按钮?苹果可能会拒绝使用分段控件作为按钮的应用程序。
至于您的具体问题,我不确定您是否可以在屏幕上显示选择器 View 之前更改所选组件。如果将 selectRow:inComponent:animated: 调用移至显示操作表之后会发生什么?您可能还应该摆脱选择器 View 上的重新加载调用,因为它会在显示时加载其数据。
关于ios - 将值设置为 UIPickerView 行错误 iOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21368982/