在iPad中的UIActionSheet中添加UIPickerView

时间:2012-01-04 15:13:32

标签: ios ipad uipickerview uiactionsheet

我有一个UIPickerView,需要让它显示在iPad的UIActionSheet中(它在iPhone中非常直接,而不是在iPad中)。

当我点击时,我在视图上有一个按钮我执行以下代码:

- (IBAction) buttonPressed:(id)sender
{
    NSLog(@"I am pressed");

    UIPickerView* picker = [[UIPickerView alloc] initWithFrame:CGRectMake(100,200,500,500)];
    picker.delegate = self;
    picker.dataSource = self;
    picker.showsSelectionIndicator = YES;
    [self.view addSubview:picker];    
}

// PickerView委托和数据源函数:

// returns the number of 'columns' to display.
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}

// returns the # of rows in each component..
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return 5;
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    return @"One";
}

但是当更改上面的代码以在actionSheet中添加PickerView时,它只显示ActionSheet的标题,但里面没有PickerView!

修改后的代码如下:

- (IBAction) buttonPressed:(id)sender
{
    NSLog(@"I am pressed");

    UIActionSheet* actionSheet = [[UIActionSheet alloc] initWithTitle:@"My PickerView"
                                              delegate:nil
                                     cancelButtonTitle:nil
                                destructiveButtonTitle:nil
                                     otherButtonTitles:nil];

    UIPickerView* picker = [[UIPickerView alloc] initWithFrame:CGRectMake(100,200,500,500)];
    picker.delegate = self;
    picker.dataSource = self;
    picker.showsSelectionIndicator = YES;

    [actionSheet addSubview:picker];
    [actionSheet showFromRect:CGRectMake(0, 0, 320, 469) inView:self.view animated:NO];

}

3 个答案:

答案 0 :(得分:4)

我卡住了,我会用UIPopoverController代替UITableViewController! ..无论如何,谢谢!

答案 1 :(得分:1)

尝试修改选择器的框架。 (100,200)on(x,y)可能不是一个好位置。

答案 2 :(得分:-4)

我认为行([actionSheet addSubview:picker];)应该是 [actionSheet addSubview:picker.view];
我遇到了同样的问题,但使用了UIWindow