2014-06-13 21 views
8

Il mio programma viene compilato correttamente, ma quando premo il pulsante, si blocca. Ecco il viewController.swift:EXC_BAD_ACCESS quando si tenta di avviare UIActionSheet in 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) 
} 
} 

appare l'errore su questa linea:

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

Ed ecco il testo di errore:

EXC_BAD_ACCESS (codice = 2, indirizzo = 0x0)

Ho provato a passare let a var, l'ho eseguito su diversi simulatori, ma il risultato è lo stesso.

risposta

6

Provato, ma incapace di capire l'eccezione. Finalmente ho ottenuto una soluzione di questo tipo:

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 Delegato

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

Grazie, funziona! A proposito, ho cambiato "var" in "let" e funziona anche! – Roman

+0

Hmm, actionSheet (myActionSheet: UIActionSheet !, clickedButtonAtIndex buttonIndex: Int) non funziona. Il codice è lo stesso, ma aggiungo queste righe all'implementazione della classe. – Roman

+0

Controllare correttamente i delegati. ha funzionato in questo codice e quindi ho postato solo –

0

Dovete finire parametri otherButtonTitles con zero. Ad esempio, nel tuo caso dovrebbe essere:

otherButtonTitles:"camera", "libary", nil 

Non è Swift specifico, è la stessa in troppo Objective-C.

Problemi correlati