如何使用UIAlertControllerreplaceUIActionSheet?

我正在维护一个基于SDK 6.0的旧iOS项目。

这个项目的一个方法叫

-(void) showComboBox:(UIView*)view:withOptions:(NSDictionary*)options

用来显示combobox。 为了实现这个目标,它使用了iOS8上弃用的UIActionSheet。

我的解决scheme是这样的:

  if (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber10_8) { UIAlertController* alertController = [UIAlertController alertControllerWithTitle:@"title" message:@"message" preferredStyle:UIAlertControllerStyleActionSheet]; UIAlertAction* item = [UIAlertAction actionWithTitle:@"item" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { //do something here //inform the selection to the WebView ... [alertController dismissViewControllerAnimated:YES completion:nil]; }]; UIAlertAction* cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { [alertController dismissViewControllerAnimated:YES completion:nil]; }]; [alertController addAction:item]; [alertController addAction:cancelAction]; //I am not sure whether it's the right way if ([view.nextResponder isKindOfClass:UIViewController.class]) { UIViewController* vc = (UIViewController*)view.nextResponder; [vc presentViewController:alertController animated:YES completion:nil]; } 

这是一个适当的解决scheme?

这是我主要关心的 :UIAlertController需要添加到UIViewController,但我只能得到UIView的指针,所以我用view.nextResponder得到我想要的,但这是一个好方法?

我用下面的代码来显示使用UIAlertViewController操作表,它的工作原理是完美的。

 - (IBAction)buttonClicked:(id)sender { UIAlertController *actionSheet = [UIAlertController alertControllerWithTitle:@"Action Sheet" message:@"Using the alert controller" preferredStyle:UIAlertControllerStyleActionSheet]; [actionSheet addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) { // Cancel button tappped. [self dismissViewControllerAnimated:YES completion:^{ }]; }]]; [actionSheet addAction:[UIAlertAction actionWithTitle:@"Delete" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) { // Distructive button tapped. [self dismissViewControllerAnimated:YES completion:^{ }]; }]]; [actionSheet addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { // OK button tapped. [self dismissViewControllerAnimated:YES completion:^{ }]; }]]; // Present action sheet. [self presentViewController:actionSheet animated:YES completion:nil]; } 

编辑:

你需要在这里获得UIViewController对象。 您可以设置全局variables或调用委托方法,也可以使用通知来获取此代码中的视图控制器对象。

最后一行代码如上。

 [self.viewController presentViewController:actionSheet animated:YES completion:nil]; 

self.viewController是一个全局variables,它会在你真正得到这个视图之前被设置。

因为你现在使用view.nextResponder 。 恐怕它可能不起作用。

我已经使用操作表更改个人资料图片。 我跟着Kampai的方法,刚刚删除dismissviewController调用,因为它是踢我的视图时按下取消或照片select视图

 UIAlertController *actionSheet = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet]; [actionSheet addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) { // Cancel button tappped do nothing. }]]; [actionSheet addAction:[UIAlertAction actionWithTitle:@"Take photo" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { // take photo button tapped. [self takePhoto]; }]]; [actionSheet addAction:[UIAlertAction actionWithTitle:@"Choose photo" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { // choose photo button tapped. [self choosePhoto]; }]]; [actionSheet addAction:[UIAlertAction actionWithTitle:@"Delete Photo" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) { // Distructive button tapped. [self deletePhoto]; }]]; 

迅速更新 –

  let actionSheet = UIAlertController.init(title: "Please choose a source type", message: nil, preferredStyle: .ActionSheet) actionSheet.addAction(UIAlertAction.init(title: "Take Photo", style: UIAlertActionStyle.Default, handler: { (action) in self.openCamera() })) actionSheet.addAction(UIAlertAction.init(title: "Choose Photo", style: UIAlertActionStyle.Default, handler: { (action) in self.showPhotoLibrary() })) actionSheet.addAction(UIAlertAction.init(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: { (action) in // self.dismissViewControllerAnimated(true, completion: nil) is not needed, this is handled automatically, //Plus whatever method you define here, gets called, //If you tap outside the UIAlertController action buttons area, then also this handler gets called. })) //Present the controller self.presentViewController(actionSheet, animated: true, completion: nil) 

你可以使用view.window.rootViewController来代替。 如果你不关心主持人,那很好。