ios - 将值设置为 UIPickerView 行错误 iOS

标签 ios objective-c uipickerview

当我点击按钮时,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/

相关文章:

ios - 如何使 UIPickerView 组件环绕?

ios - 如何在首次加载时选择 UIPickerView 中的第一行?

ios - 在 GitHub 或 Bitbucket 中支持下一版本的 SDK/框架的最佳方式是什么?

iphone - 基于苹果示例代码 TVAnimationsGestures 在基于 UITableView 的 Accordion View 中一次保持一个部分打开

ios - 使用正确的时区将 ISO8601 字符串转换为 NSDate

iphone - "Overscroll"一个 TableView

iPhone:在我们开始下一首歌曲之前,第一首歌曲不会停止

ios - 在Swift 3中向UIPicker添加渐变背景时,文本消失

iphone - iOS 上的重复推送通知

ios - 如何根据 UIImage 大小调整 UIImageView 的大小并适合屏幕的宽度