当我单击按钮时,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.
我想知道,由于此错误而行不通吗?
您正在创建一个没有标题或按钮的操作表?啊。然后,您将一个分段的控件添加为可敲击按钮?双UGH Apple可能会拒绝使用分段控件作为按钮的应用。
关于您的特定问题,我不确定是否可以在屏幕上更改选定的组件。如果您移动Selectrow:Incomponent:Animated:在显示动作表之后呼叫下来,会发生什么?您可能还应该在您的选择器视图上摆脱重新加载呼叫,因为显示时会加载其数据。