0

我有一个自定义 UIPickerView,它嵌入到 UIActionsheet 中,调用时会出现半个屏幕。效果很好。问题是我想让桶选择器在它第一次出现时显示“最可能”的结果作为选择(在所有数据都已加载到其中之后)。
在将自定义选择器嵌入到操作表中之前,我将它放在 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)];

}

4

1 回答 1

0

一种选择是创建一个可重用的选择器视图,如此处所示然后调用 @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];

希望这可以帮助!

于 2012-04-17T15:34:27.283 回答