【问题标题】:Add UIPickerView in UIActionSheet from IOS 8 not working在 IOS 8 的 UIActionSheet 中添加 UIPickerView 不起作用
【发布时间】:2014-08-13 12:33:57
【问题描述】:

我将UIPickerView 添加到UIActionsheet,但它在ios 7 中完美运行,但在ios 8 中无法运行。请帮我解决这个问题。

我在这里附上了截图。

谢谢

我正在使用以下代码。

UIActionSheet *actionSheet;
NSString *pickerType;

- (void)createActionSheet {

    if (actionSheet == nil) {
        // setup actionsheet to contain the UIPicker
        actionSheet = [[UIActionSheet alloc] initWithTitle:@"Please select" delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
        actionSheet.backgroundColor = [UIColor clearColor];

        UIToolbar *pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 40)];
        pickerToolbar.barStyle = UIBarStyleBlackOpaque;
        [pickerToolbar sizeToFit];

        UIImageView *imageObj = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 320, 40)];
        imageObj.image = [UIImage imageNamed:@"header.png"];
        [pickerToolbar addSubview:imageObj];

        UIButton *cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [cancelBtn addTarget:self action:@selector(pickerCancel:)forControlEvents:UIControlEventTouchDown];
        // [cancelBtn setImage:[UIImage imageNamed:@"btnInviteCancel.png"] forState:UIControlStateNormal];
        [cancelBtn setTitle:@"Cancel" forState:UIControlStateNormal];

        cancelBtn.frame = CGRectMake(7.0, 7.0, 65.0, 25.0);
        [pickerToolbar addSubview:cancelBtn];

        UIButton *doneBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [doneBtn addTarget:self action:@selector(pickerDone:)forControlEvents:UIControlEventTouchDown];
        //[doneBtn setImage:[UIImage imageNamed:@"btnInviteDone.png"] forState:UIControlStateNormal];
        [doneBtn setTitle:@"Done" forState:UIControlStateNormal];
        doneBtn.frame = CGRectMake(258.0, 7.0, 55.0, 25.0);
        [pickerToolbar addSubview:doneBtn];

        [actionSheet addSubview:pickerToolbar];
        [pickerToolbar release];


        UIPickerView *chPicker = [[UIPickerView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 200.0)];
        chPicker.dataSource = self;
        chPicker.delegate = self;
        chPicker.showsSelectionIndicator = YES;
        [chPicker selectRow:0 inComponent:0 animated:YES];
        [actionSheet addSubview:chPicker];
        [chPicker release];

        [actionSheet showInView:self.view];
        [actionSheet setBounds:CGRectMake(0,0,320, 464)];
    }
}

【问题讨论】:

标签: ios iphone uipickerview uiactionsheet ios8


【解决方案1】:

它不起作用,因为 Apple 更改了 UIActionSheet 的内部实现。请参考the documentation

子类化注释

UIActionSheet 并非设计为子类化,也不 您是否应该将视图添加到其层次结构中。如果您需要呈现比 UIActionSheet API 提供的自定义更多的表单,您可以创建自己的表单并使用 presentViewController:animated:completion: 以模态方式呈现它。

【讨论】:

  • 但 presentViewController 仅以模态方式显示全屏视图控制器 - 我们如何使其看起来类似于 UIActionsheet?
  • 你完全正确。看起来苹果现在确保没有人可以滥用这个组件。我已经调试了UIActionSheet,目前它是一个假的UIView,并起到代理对象的作用。它没有任何 subviews 并且没有 superview drawRect:layoutSubviews 即使在可见时也不会被调用。因此,UIActionSheet 上的任何视觉操作都会失败,并且没有简单的解决方法。同样的问题也适用于UIAlertView
【解决方案2】:

在最后两行显示 UIActionSheet 时试试这个:

[actionSheet showFromRect:CGRectMake(0,480, 320,215) inView:self.view animated:YES];
[actionSheet setBounds:CGRectMake(0,0,320, 464)];

【讨论】:

    【解决方案3】:

    根据Apple docs

    重要提示UIActionSheet 在 iOS 8 中已弃用。(请注意,UIActionSheetDelegate 也已弃用。)要在 iOS 8 及更高版本中创建和管理操作表,请改用 UIAlertControllerpreferredStyleUIAlertControllerStyleActionSheet

    Link to documentation for UIAlertController

    例如:

         UIAlertController * searchActionSheet=[UIAlertController alertControllerWithTitle:@"" message:@"" preferredStyle:UIAlertControllerStyleActionSheet];
    
                    [ searchActionSheet.view setBounds:CGRectMake(7, 180, self.view.frame.size.width, 470)];
    
                    //yourView represent the view that contains UIPickerView and toolbar
                    [searchActionSheet.view addSubview:self.yourView];
                    [self presentViewController:searchActionSheet animated:YES completion:nil];
    

    【讨论】:

    • grrr,他们必须打破旧的吗?似乎这是使用动作和警报样式的新方向。
    • 你试过了吗? addSubview 似乎不适用于 UIAlertController。
    • 我尝试了不同的方向,这导致了一个新问题。或许你有一些见识。 stackoverflow.com/questions/25975974/…
    • 我在使用 addSubview 时得到一个 EXC_BAD_ACCESS
    • 我可以看看你的代码和异常行吗?你会在 iOS 8 上部署吗?
    【解决方案4】:

    这没有显示任何内容,因为 uiactionsheet 和 uiactionsheetdelegate 已被弃用是 ios8 https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIActionSheet_Class/index.html

    您需要在 ios 8 中使用 UIAlertController。

    【讨论】:

    • 已弃用并不意味着已删除。 UIActionSheet 在 iOS 8 中运行良好
    • 实际上,它们在 iOS8+ 中无法正常工作。如果您的屏幕键盘可见,那么键盘将出现在您的 UIActionSheet 之上,因此您将无法看到您的操作表选项!您只会看到屏幕变暗。
    • @MikeGledhill 您需要在显示操作表之前将 resignFirstResponder 消息发送到您的键盘
    • 我试过了,没用。键盘只是停留在屏幕上。 DID 的工作是添加这行代码:stackoverflow.com/questions/26061964/…
    • @MikeGledhill 谢谢,我知道了,我稍后再检查,兄弟)
    【解决方案5】:
    actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self 
                                                  cancelButtonTitle:nil 
                                             destructiveButtonTitle:nil 
                                                  otherButtonTitles:nil];
    

    【讨论】:

      【解决方案6】:

      我也发现了这个问题,我有解决办法,希望对你有帮助..

      您可以在此处下载示例代码: https://www.dropbox.com/s/yrzq3hg66pjwjwn/UIPickerViewForIos8.zip?dl=0

      @interface ViewController : UIViewController <UIPickerViewDataSource, UIPickerViewDelegate>{
          UIView *maskView;
          UIPickerView *_providerPickerView;
          UIToolbar *_providerToolbar;
      }
      
      
      - (void) createPickerView {
          maskView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
          [maskView setBackgroundColor:[UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.5]];
      
          [self.view addSubview:maskView];
          _providerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height - 344, self.view.bounds.size.width, 44)];
      
          UIBarButtonItem *done = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(dismissActionSheet:)];
          _providerToolbar.items = @[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil], done];
          _providerToolbar.barStyle = UIBarStyleBlackOpaque;
          [self.view addSubview:_providerToolbar];
      
          _providerPickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height - 300, 0, 0)];
          _providerPickerView.backgroundColor = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:0.5];
          _providerPickerView.showsSelectionIndicator = YES;
          _providerPickerView.dataSource = self;
          _providerPickerView.delegate = self;
      
          [self.view addSubview:_providerPickerView];
      }
      
      - (void)dismissActionSheet:(id)sender{
          [maskView removeFromSuperview];
          [_providerPickerView removeFromSuperview];
          [_providerToolbar removeFromSuperview];
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-10-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多