I am trying to make an alert by pressing a button "delete data". My button is in a view which is inside a navigation controller. Here is my current code
class SomeViewController: UIViewController {
#IBAction func deleteData(sender: UIButton) {
let alert = UIAlertController(title: "delete Data", message: "Do you want to delete the data", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "Yes", style: UIAlertActionStyle.Default, handler: nil))
alert.addAction(UIAlertAction(title: "No", style: UIAlertActionStyle.Default, handler: nil))
}
}
But is gives me the following error:
Attempting to load the view of a view controller while it is deallocating is not allowed and may result in undefined behavior
You've created your alert view controller but you still need to present it:
self.presentViewController(alert, animated: true, completion: nil)
You can read an article on the topic on appcoda.com
for showing alert use:
self.presentViewController(myAlert, animated: true, completion: nil)
and for dismiss use:
alert.dismissViewControllerAnimated(true, completion:nil)
Related
In the following code, when my web view fails to load, the alert is properly shown with the Retry button, as expected. The alert goes away when tapping the Retry button, but the completion never gets called. Why is this?
func webView(_ webView: UIWebView, didFailLoadWithError error: Error) {
let alert = UIAlertController(title: "Network Error", message: "There was a error loading the page.", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Retry", style: .default, handler: { _ in
alert.dismiss(animated: true, completion: {
self.webView.loadHTMLString("Reloaded", baseURL: nil)
})
}));
self.present(alert, animated: true, completion: nil)
}
Don't call alert.dismiss inside the alert action. The alert controller will automatically be dismissed when the user taps one of the alert buttons.
You just need:
alert.addAction(UIAlertAction(title: "Retry", style: .default, handler: { _ in
self.webView.loadHTMLString("Reloaded", baseURL: nil)
}))
Just try it out I didn't checked ,
Change UIAlertActionStyle .default to .Cancel. Remove dismiss block give web view loading directly in handler..
Hope maybe it will help you...
Following are the lines of code that I am using in swift (Xcode) for creating a pop up.
//create the alert
let alert=UIAlertController(title: "Better Luck Next Time", message: "Try Again Later", preferredStyle: UIAlertControllerStyle.Alert)
// add an action (button)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
// show the alert
self.presentViewController(alert,
animated: true,
completion: nil)
Once the user presses the OK button in the pop up menu; I want the app to navigate to another view controller. I know that I have to put some lines of code in the handler part of UIAlertAction. But I am not sure of how to code this transition. Any one has any simple and effective ideas ??.
let alertController = UIAlertController(title: "Default AlertController", message: "A standard alert", preferredStyle: .Alert)
let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel) { (action:UIAlertAction!) in
println("you have pressed the Cancel button");
}
alertController.addAction(cancelAction)
let OKAction = UIAlertAction(title: "OK", style: .Default) { (action:UIAlertAction!) in
println("you have pressed OK button");
if let navController = self.navigationController
{
navController.popViewControllerAnimated(true)
}
}
alertController.addAction(OKAction)
self.presentViewController(alertController, animated: true, completion:nil)
I'm trying to figure out is how to create a pop up window that only appears once when you start the app and then won't appear again unless you close the app and reboot it. However, if you view the code below, you will realize that the alert will pop up every time the ViewController appears. For example, if you go to the settings tab and then return to the main ViewController, then the alert will pop up.
override func viewDidAppear(animated: Bool) {
let alertController = UIAlertController(title: "Disclaimer", message: "WARNING: Please ride carefully!", preferredStyle: UIAlertControllerStyle.Alert)
alertController.addAction(UIAlertAction(title: "Accept", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alertController, animated: true, completion: nil)
}
Just create a global variable that is a Bool. If the app is opened it starts at false. Then once the disclaimer is seen it sets the variable to true. Then present the View Controller based on the value of the variable.
var disclaimerHasBeenDisplayed = false
class ViewController {
override func viewDidAppear(animated: Bool) {
if disclaimerHasBeenDisplayed == false {
disclaimerHasBeenDisplayed = true
let alertController = UIAlertController(title: "Disclaimer", message: "WARNING: Wakeboarding is fun, however it can be highly dangerous.
Wake Dice is not liable for any injuries obtained while wakeboarding. Please ride carefully!", preferredStyle: UIAlertControllerStyle.Alert)
alertController.addAction(UIAlertAction(title: "Accept", style: UIAlertActionStyle.Default,handler: nil))
self.presentViewController(alertController, animated: true, completion: nil)
}
}
}
I want to show the alert than jump to main page, but it didn't jump and show me this warning:
Attempt to present <UITabBarController> on <SignUpViewController> which is already presenting <UIAlertController>
here is my code
let alertController = UIAlertController(title: "Success", message: "Back", preferredStyle: UIAlertControllerStyle.Alert)
alertController.addAction(UIAlertAction(title: "true", style: UIAlertActionStyle.Default,handler: nil))
self.presentViewController(alertController, animated: true, completion:nil)
let MyPage = self.storyboard?.instantiateViewControllerWithIdentifier("tabBarController")
presentViewController(MyPage!, animated: true, completion: nil)
I saw some answer let me use viewdidappear or something, but I'm still confusing and don't know how to do that, I have spend several hours not it, please help me fix this problem, Thank you very much!
You can put ur presentViewController function in alert completion handler
alertController.addAction(UIAlertAction(title: "true", style: UIAlertActionStyle.Default,handler: nil))
if u put some code in handler, the code executed when u click that "true" button.
For example,
alertController.addAction(UIAlertAction(title: "true", style: UIAlertActionStyle.Default,handler: {
(alert: UIAlertAction) -> Void in
let MyPage = self.storyboard?.instantiateViewControllerWithIdentifier("tabBarController")
self.presentViewController(MyPage!, animated: true, completion: nil)
}))
I am using swift and have a class called ViewController() that is linked to my storyboard. I am trying to get an alert view to display from another class. I am using the following code however it will not open an alert box. Any Ideas?
public class SomeClass {
func showAlert(title:String, body:String) {
var alert = UIAlertController(title: "Title", message: "Message", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil))
}
func showAlert(title:String, body:String) {
var alert = UIAlertController(title: "Title", message: "Message", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil))
var vc: ViewController = ViewController()
vc.presentViewController(alert, animated: true, completion: nil)
}
You have to call presentViewController on your UIViewController and pass in your UIAlertViewController.
In UIViewController:
self.presentViewController(alert, animated: true, completion: nil)
alert being your UIAlertViewController.