2016-08-03 67 views
6

我想用UIActionSheet为iOS8上,但它不赞成,我不知道如何使用更新的方式来使用这个......UIActionSheet上不再iOS8上

看到旧代码:

-(void)acoesDoController:(UIViewController *)controller{ 
    self.controller = controller; 
    UIActionSheet *opcoes = [[UIActionSheet alloc]initWithTitle:self.contato.nome delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:@"Delete" otherButtonTitles:@"other", nil]; 

    [opcoes showInView:controller.view]; 
} 

-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex { 

    //switch case of the buttons 

} 

为了清楚起见,在本例中,在UITableView索引中长按之后,激活操作表。

如何以正确的方式实现上面的代码?

+0

对于未来的参考,如果你看一下[UIActionSheet]的文档(https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIActionSheet_Class/)它清楚地标记被废弃从iOS8开始,它甚至会告诉你什么是替换类。当你去[UIAlertController]的文档(https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIAlertController_class/index.html#//apple_ref/occ/cl/UIAlertController)它甚至给你一个如何使用它的例子。学习阅读文档是改进的重要第一步。 – Abizern

+0

感谢您的提示!我仍然以objective-c开始。 –

回答

27

您可以使用相同的UIAlertController。

UIAlertController *actionSheet = [UIAlertController alertControllerWithTitle:@"Action Sheet" message:@"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:@"Other" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { 

      // OK button tapped. 

      [self dismissViewControllerAnimated:YES completion:^{ 
      }]; 
     }]]; 
    // Present action sheet. 
    [self presentViewController:actionSheet animated:YES completion:nil]; 
+0

Xcode显示每个方法“[self dismissViewControllerAnimated:YES completion:^ {...}没有在@interfaces中声明...我该如何解决它?...同样的情况发生在”[self presentViewController:actionSheet动画:YES完成:无];“ –

+0

你能分享你的更新代码吗? – Nilesh

+0

我已经解决了,谢谢。我试图在一个不同于tableView的类中实现它。我移动了代码,一切工作正常...但是...是否有可能为此方法创建另一个类?如果你仍然需要我可以分享我的代码。 –