ओके और कैंसिल के साथ स्विफ्ट अलर्ट देखें: किस बटन पर टैप किया गया?


105

मेरे पास स्विफ्ट में लिखे Xcode में एक अलर्ट दृश्य है और मैं यह निर्धारित करना चाहता हूं कि उपयोगकर्ता ने कौन सा बटन चुना है (यह एक पुष्टिकरण संवाद है) कुछ भी नहीं करने या कुछ निष्पादित करने के लिए।

वर्तमान में मेरे पास है:

@IBAction func pushedRefresh(sender: AnyObject) {
    var refreshAlert = UIAlertView()
    refreshAlert.title = "Refresh?"
    refreshAlert.message = "All data will be lost."
    refreshAlert.addButtonWithTitle("Cancel")
    refreshAlert.addButtonWithTitle("OK")
    refreshAlert.show()
}

मैं शायद बटन गलत का उपयोग कर रहा हूं, कृपया मुझे सही करें क्योंकि यह मेरे लिए बिल्कुल नया है।


करने के लिए नकल stackoverflow.com/questions/24195310/...
Mixaz

जवाबों:


303

यदि आप 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)

4
आप अपने उदाहरण के UIAlertActionStyle.Cancelबजाय उपयोग कर सकते हैं .Default
ट्रिस्टन वॉर्नर-स्मिथ

अगर मैं रद्द कार्रवाई में कुछ नहीं करना चाहता तो मैं कुछ नहीं लौटा सकता?
गेब्रियल रॉड्रिक्स

बेशक, तकनीकी रूप से, मैं उदाहरण के रूप में कुछ भी नहीं कर रहा हूं लेकिन लॉगिंग। लेकिन अगर मैंने लॉग हटा दिया तो मैं कुछ नहीं करूंगा।
माइकल वाइल्डरमथ

1
यह बहुत अच्छा है जब नए स्विफ्ट संस्करणों के लिए उत्तर अपडेट किए जाते हैं
ब्लैकटेगरएक्स

किसी को पता है कि "ओके" और "रद्द" कार्यों के लिए एक्सेसिबिलिटी आईडी कैसे जोड़ना है
कमलदीप सिंह भाटिया

18
var refreshAlert = UIAlertController(title: "Log Out", message: "Are You Sure to Log Out ? ", preferredStyle: UIAlertControllerStyle.Alert)

refreshAlert.addAction(UIAlertAction(title: "Confirm", style: .Default, handler: { (action: UIAlertAction!) in
    self.navigationController?.popToRootViewControllerAnimated(true)
}))

refreshAlert.addAction(UIAlertAction(title: "Cancel", style: .Default, handler: { (action: UIAlertAction!) in

    refreshAlert .dismissViewControllerAnimated(true, completion: nil)


}))

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

4

स्विफ्ट 3 के लिए अपडेट किया गया:

// फ़ंक्शन डिफिकेशन:

@IBAction func showAlertDialog(_ sender: UIButton) {
        // Declare Alert
        let dialogMessage = UIAlertController(title: "Confirm", message: "Are you sure you want to Logout?", preferredStyle: .alert)

        // Create OK button with action handler
        let ok = UIAlertAction(title: "OK", style: .default, handler: { (action) -> Void in
             print("Ok button click...")
             self.logoutFun()
        })

        // Create Cancel button with action handlder
        let cancel = UIAlertAction(title: "Cancel", style: .cancel) { (action) -> Void in
            print("Cancel button click...")
        }

        //Add OK and Cancel button to dialog message
        dialogMessage.addAction(ok)
        dialogMessage.addAction(cancel)

        // Present dialog message to user
        self.present(dialogMessage, animated: true, completion: nil)
    }

// logoutFun () फ़ंक्शन डिफिनिटॉन:

func logoutFun()
{
    print("Logout Successfully...!")
}

3

आप UIAlertController का उपयोग करके आसानी से कर सकते हैं

let alertController = UIAlertController(
       title: "Your title", message: "Your message", preferredStyle: .alert)
let defaultAction = UIAlertAction(
       title: "Close Alert", style: .default, handler: nil)
//you can add custom actions as well 
alertController.addAction(defaultAction)

present(alertController, animated: true, completion: nil)

संदर्भ: आईओएस शो अलर्ट


0

आप का उपयोग पर विचार कर सकते SCLAlertView के लिए, वैकल्पिक UIAlertView या UIAlertController

UIAlertController केवल iOS 8.x या इसके ऊपर के वर्जन पर काम करती है, SCLAlertView पुराने वर्जन को सपोर्ट करने का एक अच्छा विकल्प है।

GitHub विवरण देखने के लिए

उदाहरण:

let alertView = SCLAlertView()
alertView.addButton("First Button", target:self, selector:Selector("firstButton"))
alertView.addButton("Second Button") {
    print("Second button tapped")
}
alertView.showSuccess("Button View", subTitle: "This alert view has buttons")
हमारी साइट का प्रयोग करके, आप स्वीकार करते हैं कि आपने हमारी Cookie Policy और निजता नीति को पढ़ और समझा लिया है।
Licensed under cc by-sa 3.0 with attribution required.