Reputation: 446
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.
Upvotes: 9
Views: 1679
Reputation: 1565
You have to end otherButtonTitles parameters with nil. For example, in your case it should be:
otherButtonTitles:"camera", "libary", nil
It is not Swift specific, it is the same in objective-c too.
Upvotes: 0
Reputation: 15335
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)
}
Upvotes: 6