ios - How do I migrate from UIAlertView (deprecated in iOS8) -


i have following line of code in 1 of apps. simple uialertview. however, of ios 8, deprecated:

let alertview = uialertview(title: "oops!", message: "this feature isn't available right now", delegate: self, cancelbuttontitle: "ok") 

how update work ios 8+? believe have change uialertcotroller, though i'm not sure what.

you need use uialertcontroller instead. class documentation pretty straightforward, containing usage example in listing 1 @ beginning of doc (sure it's in objc , not in swift it's quite similar).

so use case, here how translates (with added comments):

let alert = uialertcontroller(title: "oops!", message:"this feature isn't available right now", preferredstyle: .alert) let action = uialertaction(title: "ok", style: .default) { _ in   // put here code execute when   // user taps ok button (may empty in case if that's   // informative alert) } alert.addaction(action) self.presentviewcontroller(alert, animated: true){} 

so compact code like:

let alert = uialertcontroller(title: "oops!", message:"this feature isn't available right now", preferredstyle: .alert) alert.addaction(uialertaction(title: "ok", style: .default) { _ in }) self.present(alert, animated: true){} 

where self here supposed uiviewcontroller.


additional tip: if need call code displays alert outside of context of uiviewcontroller, (where self not uiviewcontroller) can use root vc of app:

let rootvc = uiapplication.sharedapplication().keywindow?.rootviewcontroller rootvc?.presentviewcontroller(alert, animated: true){} 

(but in general it's preferable use known uiviewcontroller when have 1 — , present alerts uiviewcontrollers anyway — or try suitable 1 depending on context instead of relying on tip)


Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -