2014-07-04 2 views
6

초급 프로그래머입니다. 경고의 버튼에 작업을 추가하려고하지만 작동하지 않습니다. 경고 버튼 선택으로 레이블의 텍스트를 변경할 수 있는지 테스트하고 싶지만 작동하지 않습니다. 물론 경고와 버튼을 잘 볼 수는 있지만 버튼을 클릭 한 후에는 아무 일도 일어나지 않습니다.Swift에서 Alert의 버튼에 작업을 추가하는 방법

alertTest.delegate = self 

UIAlertView은 그것의 위임에 방법이 어떤 일을 달성하거나 이벤트 통지 호출을 의미 delegate pattern을 사용

@IBOutlet var statusLabelAlert : UILabel 

var alertTest = UIAlertView() 

@IBAction func alertButton(sender : AnyObject) { 

    alertTest.message = "Select one!" 
    alertTest.addButtonWithTitle("1st") 
    alertTest.addButtonWithTitle("2nd") 
    alertTest.addButtonWithTitle("3rd") 
    alertTest.title = "Test Alert" 
    alertTest.show() 

} 
func alertView(alertView: UIAlertView!, clickedButtonAtIndex buttonIndex: Int){ 
    switch buttonIndex{ 
    case 0: 
     statusLabelAlert.text = "1st" 
    case 1: 
     statusLabelAlert.text = "2nd" 
    case 2: 
     statusLabelAlert.text = "3rd" 
    default: 
     statusLabelAlert.text = "error" 
    } 

} 

답변

8

clickedButtonAtIndex 메서드를 호출합니까? 중단 점을 넣고 코드를 디버그하십시오. alertView의 대리자를 설정하지 않았습니다. 당신이 STH하지 않으려면

@IBOutlet var statusLabelAlert : UILabel 

var alertTest = UIAlertView() 
alertTest.delegate = self //set the delegate of alertView 

@IBAction func alertButton(sender : AnyObject) { 

alertTest.message = "Select one!" 
alertTest.addButtonWithTitle("1st") 
alertTest.addButtonWithTitle("2nd") 
alertTest.addButtonWithTitle("3rd") 
alertTest.title = "Test Alert" 
alertTest.show() 

} 
func alertView(alertView: UIAlertView!, clickedButtonAtIndex buttonIndex: Int){ 
switch buttonIndex{ 
case 0: 
    statusLabelAlert.text = "1st" 
case 1: 
    statusLabelAlert.text = "2nd" 
case 2: 
    statusLabelAlert.text = "3rd" 
default: 
    statusLabelAlert.text = "error" 
} 

} 
3

당신은 경고보기의 대리자를 설정해야합니다. UIAlertView을 사용하면 이러한 이벤트 중 하나가 사용자가 단추를 누를 때 발생합니다. 경고보기에서 사용자의 탭에 대해 말할 사람을 알기 위해서는 UIAlertViewDelegate 프로토콜을 구현하는 대리인을 지정해야합니다. 코드는 프로토콜을 구현하지만 메서드 호출을받지 않으므로 대리자가되기를 경고 메시지에 알리지 않습니다.

1

iOS 8의 경우 UIAlertController 및 UIAlertAction으로 전환해야합니다. UIAlertAction에는 버튼의 기능이 포함되어 있습니다.

3
@IBOutlet var statusLabelAlert : UILabel 
var alertTest = UIAlertView() 
@IBAction func alertButton(sender : AnyObject) 
{ 
    alertTest.delegate = self 
    alertTest.message = "Select one!" 
    alertTest.addButtonWithTitle("1st") 
    alertTest.addButtonWithTitle("2nd") 
    alertTest.addButtonWithTitle("3rd") 
    alertTest.title = "Test Alert" 
    alertTest.show() 

} 

func alertView(alertView: UIAlertView!, clickedButtonAtIndex buttonIndex: Int) 
{ 
    switch buttonIndex 
    { 
     case 0: 
     statusLabelAlert.text = "1st" 
     case 1: 
     statusLabelAlert.text = "2nd" 
     case 2: 
     statusLabelAlert.text = "3rd" 
     default: 
     statusLabelAlert.text = "error" 
    } 
} 
1
IBOutlet var statusLabelAlert : UILabel 

@IBAction func alertButton(sender : AnyObject) { 

let alert = UIAlertController(title: "Alert", message: "This is an Alert", preferredStyle: UIAlertControllerStyle.Alert) 

alert.addAction(UIAlertAction(title: "1st", style: UIAlertActionStyle.Default, handler: { (action: UIAlertAction!) in 
statusLabelAlert.text = "1st" 
})) 

alert.addAction(UIAlertAction(title: "2nd", style: UIAlertActionStyle.Default, handler: { (action: UIAlertAction!) in 
statusLabelAlert.text = "2nd" 
})) 

self.presentViewController(alert, animated: true, completion: nil) 

} 

버튼을 눌렀을 때 :

alert.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Default, handler: nil))