2016-06-09 3 views
0

UIAlert에 대한 약간의 도움이 필요합니다. "UIAlertController can only have one action with a style of UIAlertActionStyleCancel" 오류가 발생합니다. 나는 그것이 기능의 외부에서이 경고를 시작하고 있기 때문에 그것을 알고있다. 그러나 그것을 고치는 방법을 확신 할 수 없다. 조건부 인 경우 blah < 80 { 인 경우 알림에 액세스하려면 어떻게해야합니까?프로그래밍 방식으로 경고를 닫습니다.

let alertView = UIAlertController(title: "Blah", message: "", preferredStyle: UIAlertControllerStyle.Alert) 
@IBAction func blahButtonPressed(sender: AnyObject) {   

     alertView.addAction(UIAlertAction(title: "Do Something", style: .Default, handler: { (action: UIAlertAction!) in 
      // I have code here 
     })) 

     alertView.addAction(UIAlertAction(title: "Do Something 2", style: .Default, handler: { (action: UIAlertAction!) in 
      // I have code here 
     })) 

     alertView.addAction(UIAlertAction(title: "Cancel", style: .Cancel, handler: { (action: UIAlertAction!) in 
      // I have code here 
     })) 

     presentViewController(alertView, animated: true, completion: nil) 

} 

나중에 코드에서 나는 사람이 건너 오면 나는 그것을 해결하는 방법 다음은 블루투스를 통해 값을 얻을 값이 80

if blah < 80 { 
alertView.dismissViewControllerAnimated(true, completion: nil) 
} 
+0

(이상 "UIAlertActionStyleCancel"의 스타일로 하나 UIAlertAction 초래)은 "blahButton은"누를 때마다 한 시간이 추가되지 않습니다 'blahButtonPressed'가 두 번 이상 실행될 가능성이 있습니까? '취소'를 너무 많이 추가하는 것처럼 오류 메시지가 나타납니다. –

+0

아니요. 버튼 일뿐입니다. 나는 한 번만 누르면 다른 곳으로 전화하지 않는다. – KotaBear233

답변

0

미만이면 경고를 해제해야합니다.

var newAlert: AnyObject? 


@IBAction func blahButtonPressed(sender: AnyObject) {   
    let alertView = UIAlertController(title: "Blah", message: "", preferredStyle: UIAlertControllerStyle.Alert) 
    alertView.addAction(UIAlertAction(title: "Do Something", style: .Default, handler: { (action: UIAlertAction!) in 
     // I have code here 
    })) 

    alertView.addAction(UIAlertAction(title: "Do Something 2", style: .Default, handler: { (action: UIAlertAction!) in 
     // I have code here 
    })) 

    alertView.addAction(UIAlertAction(title: "Cancel", style: .Cancel, handler: { (action: UIAlertAction!) in 
     // I have code here 
    })) 

    newAlert = alertView 

     presentViewController(alertView, animated: true, completion: nil) 
} 

// 닫아야 할 때. 예를 들어 80 미만일 경우

if blah < 80 { 
    if let newAlertView = newAlert as? UIAlertController { 
     newAlertView.dismissViewControllerAnimated(true, completion: nil) 
    } 
} 
2

나는 100 % 아니지만 버튼을 한 번만 누르면 작동합니까? 그렇다면 에 @IBAction 내부에 작업을 추가하기 때문일 수 있습니다. 대신 @IBAction 외부의 UIAlertAction의 추가를 이동하고 그 내부에 경고보기 만 표시하려고 할 수 있습니다. 그래서 같이 :

let alertView = UIAlertController(title: "Blah", message: "", preferredStyle: UIAlertControllerStyle.Alert) 
alertView.addAction(UIAlertAction(title: "Do Something", style: .Default, handler: { (action: UIAlertAction!) in 
     // I have code here 
    })) 

alertView.addAction(UIAlertAction(title: "Do Something 2", style: .Default, handler: { (action: UIAlertAction!) in 
     // I have code here 
    })) 

alertView.addAction(UIAlertAction(title: "Cancel", style: .Cancel, handler: { (action: UIAlertAction!) in 
     // I have code here 
    })) 

@IBAction func blahButtonPressed(sender: AnyObject) { 
    presentViewController(alertView, animated: true, completion: nil) 
} 

이 식으로 UIAlertAction 's은 (는)

관련 문제