UIPickerController - 想要在加载时显示特定选择

时间:2012-04-17 14:38:46

标签: xcode uipickerview uiactionsheet

我有一个自定义的UIPickerView嵌入到UIActionsheet中,当调用时它出现在屏幕的一半。效果很好。问题是我想让barrelPicker在第一次出现时(在所有数据加载到其中之后)显示“最可能”的结果作为选择。 在将自定义选择器嵌入到操作表之前,我将它放在UIViewController中,并且只是在ViewController的viewDidLoad方法中调用“showProbableResults”(我的自定义方法),因为我知道那时UIPickerView会被装好并准备出发。我现在有一个相同的地方可以调用这种方法,还是需要重新考虑我的整个设计?基本上,我需要的是在加载UIPickerView之后调用它的地方。

    - (void)startWithDelegate:(UIViewController <ImageProcessingDelegate> *)sender

{
self.delegate = sender;
self.showFirstBottle = YES;
[self start];

}

- (void) start {

self.actionSheet = [[UIActionSheet alloc] initWithTitle:@"Choose Something"
                                                         delegate:self
                                                cancelButtonTitle:nil
                                           destructiveButtonTitle:nil
                                                otherButtonTitles:nil];

[self.actionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];

CGRect pickerFrame = CGRectMake(0, 40, 0, 0);

NSLog(@"1.) About to alloc the picker");

self.vintagePicker = [[UIPickerView alloc] initWithFrame:pickerFrame];
self.vintagePicker.showsSelectionIndicator = YES;
self.vintagePicker.dataSource = self;
self.vintagePicker.delegate = self;

[self.actionSheet addSubview:self.vintagePicker];
[self.vintagePicker release];

UISegmentedControl *nextButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:@"Next"]];
nextButton.momentary = YES; 
nextButton.frame = CGRectMake(260, 7.0f, 50.0f, 30.0f);
nextButton.segmentedControlStyle = UISegmentedControlStyleBar;
nextButton.tintColor = [UIColor blackColor];
[nextButton addTarget:self action:@selector(show:) forControlEvents:UIControlEventValueChanged];
[self.actionSheet addSubview:nextButton];
[nextButton release];

UISegmentedControl *backButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:@"Back"]];
backButton.momentary = YES; 
backButton.frame = CGRectMake(10, 7.0f, 50.0f, 30.0f);
backButton.segmentedControlStyle = UISegmentedControlStyleBar;
backButton.tintColor = [UIColor blackColor];
[backButton addTarget:self action:@selector(cancel:) forControlEvents:UIControlEventValueChanged];
[self.actionSheet addSubview:backButton];
[backButton release];

[self.actionSheet showInView:_delegate.parentViewController.tabBarController.view]; // show from our table view (pops up in the middle of the table)
[self.actionSheet setBounds:CGRectMake(0, 0, 320, 485)];

}

1 个答案:

答案 0 :(得分:0)

一个选项是创建一个可重复使用的选择器视图,如here所示,然后调用@optional

-(void)setInitialPickerValueToRow:(int)i inComponent:(int)j animated:(BOOL)k{
    [pickerView selectRow:i inComponent:j animated:k];
}

来自任何视图控制器或NSObject(例如您的UIActionSheetDelegate)。

您的另一个选择是设置标准的预选选项,如下所示:

self.vintagePicker = [[UIPickerView alloc] initWithFrame:pickerFrame];
self.vintagePicker.showsSelectionIndicator = YES;
self.vintagePicker.dataSource = self;
self.vintagePicker.delegate = self;
[self.actionSheet addSubview:self.vintagePicker];
[self.vintagePicker selectRow:0 inComponent:0 animated:NO];

您也可以设置不同的变量,并让selectRow:inComponent:animated:访问这些变量,而不是预设。

[self.vintagePicker selectRow:myRow inComponent:0 animated:NO];

希望这有帮助!