Skip to main content
Fixed grammar
Source Link
Pavel Chuchuva
  • 22.6k
  • 11
  • 103
  • 119

For Swift 3 & Swift 4 :

Since UIAlertView is deprecated, there is the good way for display Alert on Swift 3

let alertController = UIAlertController(title: NSLocalizedString("No network connection",comment:""), message: NSLocalizedString("connected to the internet to use this app.",comment:""), preferredStyle: .alert) let defaultAction = UIAlertAction(title: NSLocalizedString("Ok", comment: ""), style: .default, handler: { (pAlert) in //Do whatever you wantswant here }) alertController.addAction(defaultAction) self.present(alertController, animated: true, completion: nil) 

Deprecated :

This is the swift version inspired by the checked response :

Display AlertView :

 let alert = UIAlertView(title: "No network connection", message: "You must be connected to the internet to use this app.", delegate: nil, cancelButtonTitle: "Ok") alert.delegate = self alert.show() 

Add the delegate to your view controller :

class AgendaViewController: UIViewController, UIAlertViewDelegate 

When user click on button, this code will be executed :

func alertView(alertView: UIAlertView, clickedButtonAtIndex buttonIndex: Int) { } 

For Swift 3 & Swift 4 :

Since UIAlertView is deprecated, there is the good way for display Alert on Swift 3

let alertController = UIAlertController(title: NSLocalizedString("No network connection",comment:""), message: NSLocalizedString("connected to the internet to use this app.",comment:""), preferredStyle: .alert) let defaultAction = UIAlertAction(title: NSLocalizedString("Ok", comment: ""), style: .default, handler: { (pAlert) in //Do whatever you wants here }) alertController.addAction(defaultAction) self.present(alertController, animated: true, completion: nil) 

Deprecated :

This is the swift version inspired by the checked response :

Display AlertView :

 let alert = UIAlertView(title: "No network connection", message: "You must be connected to the internet to use this app.", delegate: nil, cancelButtonTitle: "Ok") alert.delegate = self alert.show() 

Add the delegate to your view controller :

class AgendaViewController: UIViewController, UIAlertViewDelegate 

When user click on button, this code will be executed :

func alertView(alertView: UIAlertView, clickedButtonAtIndex buttonIndex: Int) { } 

For Swift 3 & Swift 4 :

Since UIAlertView is deprecated, there is the good way for display Alert on Swift 3

let alertController = UIAlertController(title: NSLocalizedString("No network connection",comment:""), message: NSLocalizedString("connected to the internet to use this app.",comment:""), preferredStyle: .alert) let defaultAction = UIAlertAction(title: NSLocalizedString("Ok", comment: ""), style: .default, handler: { (pAlert) in //Do whatever you want here }) alertController.addAction(defaultAction) self.present(alertController, animated: true, completion: nil) 

Deprecated :

This is the swift version inspired by the checked response :

Display AlertView :

 let alert = UIAlertView(title: "No network connection", message: "You must be connected to the internet to use this app.", delegate: nil, cancelButtonTitle: "Ok") alert.delegate = self alert.show() 

Add the delegate to your view controller :

class AgendaViewController: UIViewController, UIAlertViewDelegate 

When user click on button, this code will be executed :

func alertView(alertView: UIAlertView, clickedButtonAtIndex buttonIndex: Int) { } 
Add swift 4 annotation
Source Link
Kevin ABRIOUX
  • 17.9k
  • 12
  • 98
  • 106

For Swift 3 & Swift 4 :

Since UIAlertView is deprecated, there is the good way for display Alert on Swift 3

let alertController = UIAlertController(title: NSLocalizedString("No network connection",comment:""), message: NSLocalizedString("connected to the internet to use this app.",comment:""), preferredStyle: .alert) let defaultAction = UIAlertAction(title: NSLocalizedString("Ok", comment: ""), style: .default, handler: { (pAlert) in //Do whatever you wants here }) alertController.addAction(defaultAction) self.present(alertController, animated: true, completion: nil) 

Deprecated :

This is the swift version inspired by the checked response :

Display AlertView :

 let alert = UIAlertView(title: "No network connection", message: "You must be connected to the internet to use this app.", delegate: nil, cancelButtonTitle: "Ok") alert.delegate = self alert.show() 

Add the delegate to your view controller :

class AgendaViewController: UIViewController, UIAlertViewDelegate 

When user click on button, this code will be executed :

func alertView(alertView: UIAlertView, clickedButtonAtIndex buttonIndex: Int) { } 

For Swift 3 :

Since UIAlertView is deprecated, there is the good way for display Alert on Swift 3

let alertController = UIAlertController(title: NSLocalizedString("No network connection",comment:""), message: NSLocalizedString("connected to the internet to use this app.",comment:""), preferredStyle: .alert) let defaultAction = UIAlertAction(title: NSLocalizedString("Ok", comment: ""), style: .default, handler: { (pAlert) in //Do whatever you wants here }) alertController.addAction(defaultAction) self.present(alertController, animated: true, completion: nil) 

Deprecated :

This is the swift version inspired by the checked response :

Display AlertView :

 let alert = UIAlertView(title: "No network connection", message: "You must be connected to the internet to use this app.", delegate: nil, cancelButtonTitle: "Ok") alert.delegate = self alert.show() 

Add the delegate to your view controller :

class AgendaViewController: UIViewController, UIAlertViewDelegate 

When user click on button, this code will be executed :

func alertView(alertView: UIAlertView, clickedButtonAtIndex buttonIndex: Int) { } 

For Swift 3 & Swift 4 :

Since UIAlertView is deprecated, there is the good way for display Alert on Swift 3

let alertController = UIAlertController(title: NSLocalizedString("No network connection",comment:""), message: NSLocalizedString("connected to the internet to use this app.",comment:""), preferredStyle: .alert) let defaultAction = UIAlertAction(title: NSLocalizedString("Ok", comment: ""), style: .default, handler: { (pAlert) in //Do whatever you wants here }) alertController.addAction(defaultAction) self.present(alertController, animated: true, completion: nil) 

Deprecated :

This is the swift version inspired by the checked response :

Display AlertView :

 let alert = UIAlertView(title: "No network connection", message: "You must be connected to the internet to use this app.", delegate: nil, cancelButtonTitle: "Ok") alert.delegate = self alert.show() 

Add the delegate to your view controller :

class AgendaViewController: UIViewController, UIAlertViewDelegate 

When user click on button, this code will be executed :

func alertView(alertView: UIAlertView, clickedButtonAtIndex buttonIndex: Int) { } 
Add Swift 3 code update
Source Link
Kevin ABRIOUX
  • 17.9k
  • 12
  • 98
  • 106

For Swift 3 :

Since UIAlertView is deprecated, there is the good way for display Alert on Swift 3

let alertController = UIAlertController(title: NSLocalizedString("No network connection",comment:""), message: NSLocalizedString("connected to the internet to use this app.",comment:""), preferredStyle: .alert) let defaultAction = UIAlertAction(title: NSLocalizedString("Ok", comment: ""), style: .default, handler: { (pAlert) in //Do whatever you wants here }) alertController.addAction(defaultAction) self.present(alertController, animated: true, completion: nil) 

Deprecated :

This is the swift version inspired by the checked response :

Display AlertView :

 let alert = UIAlertView(title: "No network connection",  message: "You must be connected to the internet to use this app.", delegate: nil, cancelButtonTitle: "Ok") alert.delegate = self alert.show() 

Add the delegate to your view controller :

class AgendaViewController: UIViewController, UIAlertViewDelegate 

When user click on button, this code will be executed :

func alertView(alertView: UIAlertView, clickedButtonAtIndex buttonIndex: Int) { } 

This is the swift version inspired by the checked response :

Display AlertView :

 let alert = UIAlertView(title: "No network connection", message: "You must be connected to the internet to use this app.", delegate: nil, cancelButtonTitle: "Ok") alert.delegate = self alert.show() 

Add the delegate to your view controller :

class AgendaViewController: UIViewController, UIAlertViewDelegate 

When user click on button, this code will be executed :

func alertView(alertView: UIAlertView, clickedButtonAtIndex buttonIndex: Int) { } 

For Swift 3 :

Since UIAlertView is deprecated, there is the good way for display Alert on Swift 3

let alertController = UIAlertController(title: NSLocalizedString("No network connection",comment:""), message: NSLocalizedString("connected to the internet to use this app.",comment:""), preferredStyle: .alert) let defaultAction = UIAlertAction(title: NSLocalizedString("Ok", comment: ""), style: .default, handler: { (pAlert) in //Do whatever you wants here }) alertController.addAction(defaultAction) self.present(alertController, animated: true, completion: nil) 

Deprecated :

This is the swift version inspired by the checked response :

Display AlertView :

 let alert = UIAlertView(title: "No network connection",  message: "You must be connected to the internet to use this app.", delegate: nil, cancelButtonTitle: "Ok") alert.delegate = self alert.show() 

Add the delegate to your view controller :

class AgendaViewController: UIViewController, UIAlertViewDelegate 

When user click on button, this code will be executed :

func alertView(alertView: UIAlertView, clickedButtonAtIndex buttonIndex: Int) { } 
Source Link
Kevin ABRIOUX
  • 17.9k
  • 12
  • 98
  • 106
Loading