Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 282 Vote(s) - 3.46 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How would I create a UIAlertView in Swift?

#21
**on IOS 9, you can do this**

let alert = UIAlertController(title: "Alert", message: "Message", preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "Click", style: UIAlertActionStyle.default, handler: nil))
self.present(alert, animated: true, completion: nil)
Reply

#22
For **SWIFT4**, I think, extending `UIViewController` and creating a reusable confirmation control is the most elegant way.

You can extend the `UIViewController` as below:

extension UIViewController {

func AskConfirmation (title:String, message:String, completion:@escaping (_ result:Bool) -> Void) {
let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.alert)
self.present(alert, animated: true, completion: nil)

alert.addAction(UIAlertAction(title: "Ok", style: .default, handler: { action in
completion(true)
}))

alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: { action in
completion(false)
}))
}
}

Then you can use it anytime:

AskConfirmation(title: "YOUR MESSAGE TITLE", message: "YOUR MESSAGE") { (result) in
if result { //User has clicked on Ok

} else { //User has clicked on Cancel

}
}
Reply

#23
let alertController = UIAlertController(title: "Select Photo", message: "Select atleast one photo", preferredStyle: .alert)
let action1 = UIAlertAction(title: "From Photo", style: .default) { (action) in
print("Default is pressed.....")
}
let action2 = UIAlertAction(title: "Cancel", style: .cancel) { (action) in
print("Cancel is pressed......")
}
let action3 = UIAlertAction(title: "Click new", style: .default) { (action) in
print("Destructive is pressed....")

}
alertController.addAction(action1)
alertController.addAction(action2)
alertController.addAction(action3)
self.present(alertController, animated: true, completion: nil)

}
Reply

#24






// UIAlertView is deprecated. Use UIAlertController
// title = title of the alert view.
// message = Alert message you want to show.
// By tap on "OK" , Alert view will dismiss.

UIAlertView(title: "Alert", message: "Enter Message here.", delegate: nil, cancelButtonTitle: "OK").show()


Reply

#25
try This.
Put Bellow Code In Button.


let alert = UIAlertController(title: "Your_Title_Text", message: "Your_MSG", preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "Your_Text", style: UIAlertActionStyle.default, handler: nil))
self.present(alert, animated:true, completion: nil)
Reply

#26
Below is the reusable code for alert view and action sheet, Just write one line to show alert anywhere in application

class AlertView{

static func show(title:String? = nil,message:String?,preferredStyle: UIAlertControllerStyle = .alert,buttons:[String] = ["Ok"],completionHandler:@escaping (String)->Void){
let alert = UIAlertController(title: title, message: message, preferredStyle: preferredStyle)

for button in buttons{

var style = UIAlertActionStyle.default
let buttonText = button.lowercased().replacingOccurrences(of: " ", with: "")
if buttonText == "cancel"{
style = .cancel
}
let action = UIAlertAction(title: button, style: style) { (_) in
completionHandler(button)
}
alert.addAction(action)
}

DispatchQueue.main.async {
if let app = UIApplication.shared.delegate as? AppDelegate, let rootViewController = app.window?.rootViewController {
rootViewController.present(alert, animated: true, completion: nil)
}

}
}
}



**Usage :**

class ViewController: UIViewController {

override func viewWillAppear(_ animated: Bool) {
AlertView.show(title: "Alert", message: "Are you sure ?", preferredStyle: .alert, buttons: ["Yes","No"]) { (button) in
print(button)
}
}

}

Reply

#27
SWIFT 4 : Simply create a extension to UIViewController as follows:

extension UIViewController {
func showSuccessAlert(withTitle title: String, andMessage message:String) {
let alert = UIAlertController(title: title, message: message,
preferredStyle: UIAlertController.Style.alert)
alert.addAction(UIAlertAction(title: "OK".localized, style:
UIAlertAction.Style.default, handler: nil))
self.present(alert, animated: true, completion: nil)
}
}

Now in your ViewController, directly call above function as if they are provided by UIViewController.

yourViewController.showSuccessAlert(withTitle:
"YourTitle", andMessage: "YourCustomTitle")
Reply

#28
You can use this simple extension with **n number** of buttons and associated actions swift4 and above

extension UIViewController {
func popupAlert(title: String?, message: String?, actionTitles:[String?], actions:[((UIAlertAction) -> Void)?]) {
let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
for (index, title) in actionTitles.enumerated() {
let action = UIAlertAction(title: title, style: .default, handler: actions[index])
alert.addAction(action)
}
self.present(alert, animated: true, completion: nil)
}
}




you can use it like ,

self.popupAlert(title: "Message", message: "your message", actionTitles: ["first","second","third"], actions:[
{action1 in
//action for first btn click
},
{action2 in
//action for second btn click
},
{action3 in
//action for third btn click
}, nil])
Reply

#29
#One Button

[![One Button Screenshot][1]][1]

class ViewController: UIViewController {

@IBAction func showAlertButtonTapped(_ sender: UIButton) {

// create the alert
let alert = UIAlertController(title: "My Title", message: "This is my message.", preferredStyle: UIAlertController.Style.alert)

// add an action (button)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertAction.Style.default, handler: nil))

// show the alert
self.present(alert, animated: true, completion: nil)
}
}

#Two Buttons

[![Two Button Alert Screenshot][2]][2]

class ViewController: UIViewController {

@IBAction func showAlertButtonTapped(_ sender: UIButton) {

// create the alert
let alert = UIAlertController(title: "UIAlertController", message: "Would you like to continue learning how to use iOS alerts?", preferredStyle: UIAlertController.Style.alert)

// add the actions (buttons)
alert.addAction(UIAlertAction(title: "Continue", style: UIAlertAction.Style.default, handler: nil))
alert.addAction(UIAlertAction(title: "Cancel", style: UIAlertAction.Style.cancel, handler: nil))

// show the alert
self.present(alert, animated: true, completion: nil)
}
}

#Three Buttons

[![enter image description here][3]][3]

class ViewController: UIViewController {

@IBAction func showAlertButtonTapped(_ sender: UIButton) {

// create the alert
let alert = UIAlertController(title: "Notice", message: "Lauching this missile will destroy the entire universe. Is this what you intended to do?", preferredStyle: UIAlertController.Style.alert)

// add the actions (buttons)
alert.addAction(UIAlertAction(title: "Remind Me Tomorrow", style: UIAlertAction.Style.default, handler: nil))
alert.addAction(UIAlertAction(title: "Cancel", style: UIAlertAction.Style.cancel, handler: nil))
alert.addAction(UIAlertAction(title: "Launch the Missile", style: UIAlertAction.Style.destructive, handler: nil))

// show the alert
self.present(alert, animated: true, completion: nil)
}
}

#Handling Button Taps

The `handler` was `nil` in the above examples. You can replace `nil` with a [closure][5] to do something when the user taps a button. For example:

alert.addAction(UIAlertAction(title: "Launch the Missile", style: UIAlertAction.Style.destructive, handler: { action in

// do something like...
self.launchMissile()

}))

#Notes

- Multiple buttons do not necessarily need to use different `UIAlertAction.Style` types. They could all be `.default`.
- For more than three buttons consider using an Action Sheet. The setup is very similar. [Here is an example.][6]


[1]:

[2]:

[3]:

[4]:

[5]:

[To see links please register here]

[6]:

[To see links please register here]

Reply

#30
**SwiftUI** on Swift 5.x and Xcode 11.x

```swift
import SwiftUI

struct ContentView: View {

@State private var isShowingAlert = false

var body: some View {
VStack {
Button("A Button") {

self.isShowingAlert.toggle()
}
.alert(isPresented: $isShowingAlert) { () -> Alert in
Alert(
title: Text("Alert"),
message: Text("This is an alert"),
dismissButton:
.default(
Text("OK"),
action: {
print("Dismissing alert")
}
)
)
}

}
.padding()
}
}

struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
```
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through