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

我的程序编译成功,但是当我按下按钮时,它会崩溃.这是viewController. swift
import UIKit

class ViewController: UIViewController,UIActionSheetDelegate{

@IBOutlet var button:UIButton

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view,typically from a nib.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // dispose of any resources that can be recreated.
}


@IBAction func buttonpressed(AnyObject) {
    let choice = UIActionSheet(title: "Select source",delegate: self,cancelButtonTitle: "Cancel",destructiveButtonTitle: nil,otherButtonTitles:"camera","libary")
    choice.showInView(self.view)
}
}

错误出现在这一行:

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

这是错误文本:

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

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

解决方法

试过,但无法弄清楚异常.最后我得到了这样的解决方案:
var myActionSheet:UIActionSheet = UIActionSheet()

        var title : String? = "Select Source"
        myActionSheet.title  = title
        myActionSheet.delegate = self
        myActionSheet.addButtonWithTitle("camera")
        myActionSheet.addButtonWithTitle("Library")
        myActionSheet.addButtonWithTitle("Cancel")

        myActionSheet.cancelButtonIndex = 2 
        myActionSheet.showInView(self.view)

UIActionSheet代表

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

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...