Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a simple Alert in swift

It looks like alertstatus cannot be used in swift.

Can you guys please help me implementing an alternative solution to the code below?

  [self alertStatus:@"Message!" :@"Alert title" :0];
like image 987
Gokhan Dilek Avatar asked Jun 07 '14 14:06

Gokhan Dilek


People also ask

How do I create a custom alert in Swift?

Let's StartOpen Main. storyboard and add two buttons on View controller Scene. These buttons are used to display two types of alert i.e single button and two button alert. Select Cocoa Touch Class from Source and click Next.

How do I present a custom alert in SwiftUI?

Build and present custom alerts to your users Presenting system alerts on SwiftUI is super simple. Just call an instance method alert and pass a few parameters, and you are done. But the system alerts that come up are too basic with the non-customizable default design.


2 Answers

XCODE 10, IOS 12 And Swift 4 and above :

1. simple alert view with no action on buttons in an alert.

let alert = UIAlertController(title: "Error", message: "Something Went Wrong", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Ok", style: .default))
self.present(alert, animated: true)

2. Alert with actions on ok and cancel button:

let refreshAlert = UIAlertController(title: "Punch Out", message: "You Will Will Punch Out", preferredStyle: UIAlertController.Style.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")
            refreshAlert .dismiss(animated: true, completion: nil)
   }))

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

For more customization Refer to this StackOverflow Answer

like image 165
NickCoder Avatar answered Oct 21 '22 06:10

NickCoder


Swift 3 iOS 10

 let alert = UIAlertController(title: "Title", message: "Message", preferredStyle: UIAlertControllerStyle.alert)
            alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.default, handler: nil))
            self.present(alert, animated: true, completion: nil)
like image 25
JP Aquino Avatar answered Oct 21 '22 07:10

JP Aquino