Greetings Programs!
What type of errors are you getting? I'm using XCode Version 8.3.3 (8E3004b) with a build target of 10.3 in my app and STV code only and no issues.
You probably have already found this out since your original post but the UIAlertView was deprecated in iOS 9. You need to replace it with the UIAlertController and UIAlertAction.
I created an extension for UINavigationController (and supporting extensions) that makes things easier for me to display an alert:
import Foundation
extension UINavigationController
{
func alert(title: String, message: String) {
self.alert(title:title, message:message, handler:nil)
}
func alert(title: String, message: String, handler: ((UIAlertAction) -> Swift.Void)? = nil) {
self.alert(title: title, message: message, buttonTitle: "OK", handler: handler)
}
func alert(title: String, message: String, buttonTitle: String, handler: ((UIAlertAction) -> Swift.Void)? = nil) {
self.alert(title: title, message: message, okButtonTitle: buttonTitle, cancelButtonTitle: nil, okHandler: handler, cancelButtonHandler: nil)
}
func alert(title: String, message: String, okButtonTitle: String, cancelButtonTitle: String?, okHandler: ((UIAlertAction) -> Swift.Void)? = nil, cancelButtonHandler: ((UIAlertAction) -> Swift.Void)? = nil) {
let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: okButtonTitle, style: UIAlertActionStyle.default, handler: okHandler))
if (cancelButtonTitle != nil) {
alert.addAction(UIAlertAction(title: cancelButtonTitle, style: UIAlertActionStyle.default, handler: cancelButtonHandler))
}
present(alert, animated: true, completion: nil)
}
}
import Foundation
extension UIApplication {
class func topViewController(_ viewController: UIViewController? = UIApplication.shared.keyWindow?.rootViewController) -> UIViewController? {
if let nav = viewController as? UINavigationController {
return topViewController(nav.visibleViewController)
}
if let tab = viewController as? UITabBarController {
if let selected = tab.selectedViewController {
return topViewController(selected)
}
}
if let presented = viewController?.presentedViewController {
return topViewController(presented)
}
return viewController
}
}
import UIKit
extension UIViewController {
var topNavigationController: UINavigationController? {
get {
return UIApplication.topViewController()?.navigationController
}
}
}
and some examples of it's usage:
topNavigationController!.alert(title: "Error", message: "All fields are required.")
topNavigationController!.alert(title: "Password Change Required", message: "You are required to create your own password on initial login.", handler: { [weak self]
action in
self?.performSegue(withIdentifier: "ChangePassword", sender: nil)
})
tableViewModel.sectionActions.willDeleteItem = { [weak self]
(itemsSection, item, indexPath) -> Bool in
guard let boundObject = item as? NSDictionary else {
return false
}
let title = "Delete Entry"
let message = "Do you wish to delete this timesheet entry?"
let confirmAction = "DELETE"
itemsSection?.ownerTableViewModel.viewController.navigationController?.alert(title: title, message: message, okButtonTitle: confirmAction, cancelButtonTitle: "Cancel", okHandler: { [weak self]
action in
self?.deleteTimesheetEntry(boundObject)
}, cancelButtonHandler: nil)
return false
}
Wg
Edited by wizgod, 12 July 2017 - 08:25 AM.