यदि आप iOS8 का उपयोग कर रहे हैं, तो आपको UIAlertController का उपयोग करना चाहिए - UIAlertView को हटा दिया गया है ।
इसका उपयोग कैसे करें इसका एक उदाहरण इस प्रकार है:
var refreshAlert = UIAlertController(title: "Refresh", message: "All data will be lost.", preferredStyle: UIAlertControllerStyle.Alert)
refreshAlert.addAction(UIAlertAction(title: "Ok", style: .Default, handler: { (action: UIAlertAction!) in
print("Handle Ok logic here")
}))
refreshAlert.addAction(UIAlertAction(title: "Cancel", style: .Cancel, handler: { (action: UIAlertAction!) in
print("Handle Cancel Logic here")
}))
presentViewController(refreshAlert, animated: true, completion: nil)
जैसा कि आप देख सकते हैं कि UIAlertAction के लिए ब्लॉक हैंडलर बटन प्रेस को हैंडल करते हैं। एक महान ट्यूटोरियल यहां है (हालांकि यह ट्यूटोरियल स्विफ्ट का उपयोग करके नहीं लिखा गया है):
http://hayageek.com/uialertcontroller-example-ios/
स्विफ्ट 3 अपडेट:
let refreshAlert = UIAlertController(title: "Refresh", message: "All data will be lost.", preferredStyle: UIAlertControllerStyle.alert)
refreshAlert.addAction(UIAlertAction(title: "Ok", style: .default, handler: { (action: UIAlertAction!) in
print("Handle Ok logic here")
}))
refreshAlert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: { (action: UIAlertAction!) in
print("Handle Cancel Logic here")
}))
present(refreshAlert, animated: true, completion: nil)
स्विफ्ट 5 अपडेट:
let refreshAlert = UIAlertController(title: "Refresh", message: "All data will be lost.", preferredStyle: UIAlertControllerStyle.alert)
refreshAlert.addAction(UIAlertAction(title: "Ok", style: .default, handler: { (action: UIAlertAction!) in
print("Handle Ok logic here")
}))
refreshAlert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: { (action: UIAlertAction!) in
print("Handle Cancel Logic here")
}))
present(refreshAlert, animated: true, completion: nil)