从IOS 8在UIActionSheet中添加UIPickerView无法正常工作

时间:2014-06-23 12:51:18

标签: ios iphone uipickerview uiactionsheet ios8

我正在将UIPickerView添加到UIActionsheet,但它在ios 7中完美地工作,但在ios 8中无效。请帮我解决。

这里我附上了截图。enter image description here

由于

我正在使用以下代码。

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)];
    }
}

7 个答案:

答案 0 :(得分:21)

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

  

子类注释

     

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

答案 1 :(得分:21)

根据Apple docs

  

重要:iOS 8中不推荐使用UIActionSheet。(请注意,UIActionSheetDelegate也已弃用。)要在iOS 8及更高版本中创建和管理操作表,请改用UIAlertController preferredStyle UIAlertControllerStyleActionSheet。{/ p>

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];

答案 2 :(得分:11)

开箱即用的解决方案具有完全相同的行为 - ActionSheetPicker-3.0

原始版本使用UIActionsheet,但在iOS 8版本中,我升级此项目并用简单视图替换UIActionsheet,但动画和视图看起来完全相同。

animation

ActionSheetPicker-3.0

答案 3 :(得分:8)

我也发现了这个问题,我有一个解决方案,希望它可以帮到你..

您可以在此处下载示例代码: 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];
}

答案 4 :(得分:0)

这并没有显示任何内容,因为uiactionsheet&amp;不推荐使用uiactionsheetdelegate是ios8 https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIActionSheet_Class/index.html

您需要在ios 8中使用UIAlertController。

答案 5 :(得分:-2)

actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self 
                                              cancelButtonTitle:nil 
                                         destructiveButtonTitle:nil 
                                              otherButtonTitles:nil];

答案 6 :(得分:-4)

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

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