ios – 尝试在Swift中初始化UIActionSheet时的EXC_BAD_ACCESS

前端之家收集整理的这篇文章主要介绍了ios – 尝试在Swift中初始化UIActionSheet时的EXC_BAD_ACCESS前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我的程序编译成功,但是当我按下按钮时,它会崩溃.这是viewController. swift
  1. import UIKit
  2.  
  3. class ViewController: UIViewController,UIActionSheetDelegate{
  4.  
  5. @IBOutlet var button:UIButton
  6.  
  7. override func viewDidLoad() {
  8. super.viewDidLoad()
  9. // Do any additional setup after loading the view,typically from a nib.
  10. }
  11.  
  12. override func didReceiveMemoryWarning() {
  13. super.didReceiveMemoryWarning()
  14. // Dispose of any resources that can be recreated.
  15. }
  16.  
  17.  
  18. @IBAction func buttonPressed(AnyObject) {
  19. let choice = UIActionSheet(title: "Select source",delegate: self,cancelButtonTitle: "Cancel",destructiveButtonTitle: nil,otherButtonTitles:"camera","libary")
  20. choice.showInView(self.view)
  21. }
  22. }

错误出现在这一行:

  1. let choice = UIActionSheet(title: "Select source","library")

这是错误文本:

EXC_BAD_ACCESS(代码= 2,地址= 0x0)

我试图将let切换到var,在不同的模拟器上运行它,但结果是一样的.

解决方法

试过,但无法弄清楚异常.最后我得到了这样的解决方案:
  1. var myActionSheet:UIActionSheet = UIActionSheet()
  2.  
  3. var title : String? = "Select Source"
  4. myActionSheet.title = title
  5. myActionSheet.delegate = self
  6. myActionSheet.addButtonWithTitle("camera")
  7. myActionSheet.addButtonWithTitle("Library")
  8. myActionSheet.addButtonWithTitle("Cancel")
  9.  
  10. myActionSheet.cancelButtonIndex = 2
  11. myActionSheet.showInView(self.view)

UIActionSheet代表

  1. func actionSheet(myActionSheet: UIActionSheet!,clickedButtonAtIndex buttonIndex: Int){
  2. println("the index is %d",buttonIndex)
  3. }

猜你在找的iOS相关文章