在UIActionSheet从IOS 8添加UIPickerView不起作用

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

这不起作用,因为Apple更改了UIActionSheet内部实现。 请参考文档 :

子类化笔记

UIActionSheet不是被devise为子类, 也不 应该添加视图到它的层次结构中 。 如果您需要提供比UIActionSheet API提供更多定制的工作表,您可以创build自己的模型并使用presentViewController呈现它:animated:completion :.

根据苹果文档 ,

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

链接到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]; 

现成的解决scheme具有完全相同的行为 – ActionSheetPicker-3.0

原始版本使用UIActionsheet ,但与iOS 8版本我升级这个项目,并用简单的视图replaceUIActionsheet,但animation和视图看起来完全一样。

动画

ActionSheetPicker-3.0

我也发现这个问题,我有一个解决scheme,我希望它可以帮助你..

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

这不显示任何内容,因为uiactionsheet&uiactionsheetdelegate是弃用的是ios8 https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIActionSheet_Class/index.html

你需要在ios 8中使用UIAlertController。

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

当在最后2行中显示UIActionSheet时试试这个:

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