将值设置为UIPickerView行错误iOS

时间:2014-01-26 20:47:50

标签: 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.

我想知道,因为这个错误,它不能正常工作吗?

1 个答案:

答案 0 :(得分:1)

您是否正在创建没有标题或按钮的操作表?啊。然后你添加一个分段控件作为一个可点击按钮? Double ugh Apple可能会拒绝使用分段控件作为按钮的应用程序。

至于您的具体问题,我不确定您是否可以在选择器视图中更改所选组件,然后才能在屏幕上显示。移动selectRow会发生什么:inComponent:animated:在显示操作表后调用?您可能还应该在选择器视图中删除重新加载调用,因为它会在显示时加载它的数据。