엑스코드(Xcode)/Swift 4

[Swift 4] 경고창 (Alert) - UIAlertController

Clein8 2018. 12. 19. 23:14


UIAlertController - Apple Developer Documentation

Swift

// MARK: UIAlertController - Apple Developer Documentation let alert = UIAlertController(title: "My Alert", message: "This is an alert.", preferredStyle: .alert) alert.addAction(UIAlertAction(title: NSLocalizedString("OK", comment: "Default action"), style: .default, handler: { _ in NSLog("The \"OK\" alert occured.") })) self.present(alert, animated: true, completion: nil)




Title + OK

Swift 4

let alert = UIAlertController(title: "Alert", message: nil, preferredStyle: .alert) let action = UIAlertAction(title: "OK", style: .default, handler: nil) alert.addAction(action) present(alert, animated: true, completion: nil)




Title + message + OK

Swift 4

let alert = UIAlertController(title: "Alert", message: "message", preferredStyle: .alert) let action = UIAlertAction(title: "OK", style: .default, handler: nil) alert.addAction(action) present(alert, animated: true, completion: nil)




Title + message + OK + Cancel

Swift 4

let alert = UIAlertController(title: "Alert", message: "message", preferredStyle: .alert) let action = UIAlertAction(title: "OK", style: .default, handler: nil) let cancel = UIAlertAction(title: "Cancel", style: .cancel, handler: nil) alert.addAction(action) alert.addAction(cancel) present(alert, animated: true, completion: nil)




Title + message + Delete + Cancel

Swift 4

let alert = UIAlertController(title: "Alert", message: "message", preferredStyle: .alert) let action = UIAlertAction(title: "Delete", style: .destructive) { (action) in // Delete Code } let cancel = UIAlertAction(title: "Cancel", style: .cancel, handler: nil) alert.addAction(action) alert.addAction(cancel) present(alert, animated: true, completion: nil)




Etc


Action 버튼을 추가하지 않으면 강제종료 시켜야 하는 무서운 일이 벌어집니다. ㅋㅋ