2017-02-04 65 views
0

這是警報的代碼。問題是,當用戶按下「Ja」這個意思是「Yes」的英文單詞時,我想繼續使用另一個VC。如何使用AlertView繼續?

@IBAction func TillbakaAction(_ sender: UIButton) 
{ 
    createAlert(title: "Är du säker på att du vill börja om?", message: "Ifyllda betyg nollställs") 


} 
func createAlert (title:String, message:String) 
{ 
    let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.alert) 

    //CREATING ON BUTTON 
    alert.addAction(UIAlertAction(title: "Ja", style: UIAlertActionStyle.default, handler: { (action) in 
     alert.dismiss(animated: true, completion: nil) 
     print ("Jag vill gå tillbaka") 



       })) 

    alert.addAction(UIAlertAction(title: "Nej", style: UIAlertActionStyle.default, handler: { (action) in 
     alert.dismiss(animated: true, completion: nil) 
     print("Nej, jag vill inte gå tillbaka") 
    })) 

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

回答

1

無需調用dismiss與警報,當你按下的AlertController任何行動,它會自動解除警報。

所以只需簡單地添加performSegue(withIdentifier:sender:)與您的行動。

let alert = UIAlertController(title: title, message: message, preferredStyle: .alert) 

alert.addAction(UIAlertAction(title: "Ja", style: .default, handler: { (action) in 

    print ("Jag vill gå tillbaka") 
    // call the segue at hare 
    self.performSegue(withIdentifier:"SegueIdentifer", sender: nil) 
})) 

alert.addAction(UIAlertAction(title: "Nej", style: .default, handler: { (action) in 

    print("Nej, jag vill inte gå tillbaka") 
})) 

self.present(alert, animated: true) 
+0

沒有你,我會做什麼。謝謝 – theswed

+0

@ theswed歡迎伴侶:) –

0
@IBAction func TillbakaAction(_ sender: UIButton) 
{ 
    createAlert(title: "Är du säker på att du vill börja om?", message: "Ifyllda betyg nollställs") 


} 
func createAlert (title:String, message:String) 
{ 
    let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.alert) 

    //CREATING ON BUTTON 
    alert.addAction(UIAlertAction(title: "Ja", style: UIAlertActionStyle.default, handler: { (action) in 
     alert.dismiss(animated: true, completion: nil) 
     print ("Jag vill gå tillbaka") 
// call the segue at hare 


       })) 

    alert.addAction(UIAlertAction(title: "Nej", style: UIAlertActionStyle.default, handler: { (action) in 
     alert.dismiss(animated: true, completion: nil) 
     print("Nej, jag vill inte gå tillbaka") 
    })) 

    self.present(alert, animated: true, completion: nil) 
+0

它的工作表示感謝 – theswed