【问题标题】:Adding a UIPickerView inside UIActionSheet in iPad在 iPad 的 UIActionSheet 中添加 UIPickerView
【发布时间】:2012-01-04 15:13:31
【问题描述】:

我有一个 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的header,但里面没有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];

}

【问题讨论】:

    标签: ios ipad uipickerview uiactionsheet


    【解决方案1】:

    我卡住了,我要在里面使用 UIPopoverControllerUITableViewController! .. 还是谢谢!

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • 当我将pickerView initLine 更改为UIPickerView* picker = [[UIPickerView alloc] initWithFrame:CGRectMake(0,0,320,469)]; 时,我现在可以看到ActionSheet 内的PickerView 非常小的一部分,但是actionSheet 显示为一个非常小的正方形,我怎样才能使它更大,我正在尝试更改它的宽度和高度,但似乎不起作用!
      • 尝试将cancelButtonTitle设置为某个NSString,看看大小是否有变化。
      • 我糟透了,我要用UIPopoverControllerUITableViewController 代替! .. 无论如何,谢谢!
      【解决方案3】:

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

      【讨论】:

      • UIPickerView 继承自 UIView;它没有自己的属性view
      猜你喜欢
      • 1970-01-01
      • 2014-08-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多