2014-06-13 2 views
8

내 프로그램이 성공적으로 컴파일되지만 버튼을 누르면 프로그램이 충돌합니다. 오류이 줄에 나타납니다Swift에서 UIActionSheet를 초기화하려고 할 때 EXC_BAD_ACCESS

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) 
} 
} 

: 여기 viewController.swift입니다

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

을 그리고 여기에 오류 텍스트입니다 :

EXC_BAD_ACCESS (코드 = 2, 주소 = 0x0으로)

var로 바꾸려고했지만 다른 시뮬레이터에서 실행했지만 결과는 같습니다.

답변

6

시도했지만 예외를 알아낼 수 없습니다.

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 위임 당신은 무기 호 otherButtonTitles 매개 변수를 종료해야

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

고맙습니다. 작동합니다! Btw, 나는 "var"를 "let"으로 변경했고 너무 효과적입니다! – Roman

+0

흠, actionSheet (myActionSheet : UIActionSheet !, clickedButtonAtIndex buttonIndex : Int)가 작동하지 않습니다. 코드는 동일하지만 클래스 구현에이 행을 추가합니다. – Roman

+0

대리인을 올바르게 확인하십시오. 이 코드를 실행 한 다음 –

0

: 마지막으로 나는이 같은 솔루션을 얻었다. 예를 들어 귀하의 경우 다음과 같아야합니다 :

이것은 구체적이지 않으며 객관적으로도 동일합니다.