Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EXC_BAD_ACCESS when trying to init UIActionSheet in Swift

My program compiles successfully, but when I press the button, it crashes. Here's the 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)
}
}

The error appears on this line:

let choice = UIActionSheet(title: "Select source", delegate: self, cancelButtonTitle: "Cancel", destructiveButtonTitle: nil, otherButtonTitles:"camera", "library")

And here's error text:

EXC_BAD_ACCESS (code=2, address=0x0)

I tried to switch let to var, ran it on different simulators, but the result is same.

like image 500
lazyd3v Avatar asked Jun 13 '14 05:06

lazyd3v


1 Answers

Tried, but unable to figure out the exception. Finally I got a solution like this :

  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 Delegate

func actionSheet(myActionSheet: UIActionSheet!, clickedButtonAtIndex buttonIndex: Int){
        println("the index is %d", buttonIndex)
    }
like image 184
Kumar KL Avatar answered Sep 28 '22 11:09

Kumar KL