I am trying to print the error to the user using alertview.
Here is my code :::
if error != nil{
let DisplayAlert = UIAlertController(title: "Error!!", message: error?.description, preferredStyle: .Alert)
DisplayAlert.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil))
}
When I run the code I see the following in the console and I do not see any alert displayed to user.
"Attempting to load the view of a view controller while it is deallocating is not allowed and may result in undefined behavior"
What should i do to fix this issue.? Pls help.
Thanks !!!
Looks like you have forgot to present the alert
if error != nil{
let DisplayAlert = UIAlertController(title: "Error!!", message: error?.description, preferredStyle: .Alert)
DisplayAlert.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil))
self.presentViewController(DisplayAlert, animated: true, completion: nil)
}
Related
I am trying to create and display an UIalert for my app. The problem with this alert is that I am trying to make and display an alert by writing the code in a class that is outside of the view controller. (Doing it in the class outside the view controller is important because of the other statements I have in the class UserApi pertaining to the profile image for which my alert is intended to be for.) Any idea on how to do this so I don't get the error
"Warning: Attempt to present <UIAlertController.....> : whose view is not in the window hierarchy!"?
I have already tried using UIApplication.shared.keyWindow?.rootViewController?.present(alertController, animated: true) instead of present(alertController, animated: true). I get the same error.
class UserApi : UIAlertController {
func signUp()....
//detect if user hasn't added a profile image and send an alert indicating the user must add a profile image
guard let imageSelected = image else {
let alertController = UIAlertController(title: "Profile Image Required",
message: "Please add a profile image to proceed." , preferredStyle: .alert)
let OKAction = UIAlertAction(title: "OK", style: .default)
alertController.addAction(OKAction)
present(alertController, animated: true)
}
......
In Guard let statement, return statement is required, Please replace the following code and retry. It is working fine.
guard let imageSelected = image else {
let alertController = UIAlertController(title: "Profile Image Required",
message: "Please add a profile image to proceed." , preferredStyle: .alert)
let OKAction = UIAlertAction(title: "OK", style: .default)
alertController.addAction(OKAction)
present(alertController, animated: true)
return
}
pass controller with signup function
like
func signUp(controller:UIViewController){
let alertController = UIAlertController(title: "Profile Image Required",
message: "Please add a profile image to proceed." , preferredStyle: .alert)
let OKAction = UIAlertAction(title: "OK", style: .default)
alertController.addAction(OKAction)
controller.present(alertController, animated: true)
}
I am trying to display an alert that takes user input and then pops up the text that has been given
#IBAction func forgotPassword(_ sender: Any) {
//1. Create the alert controller.
let alert = UIAlertController(title: "Email Recovery", message: "Enter your email to recover your account", preferredStyle: .alert)
//2. Add the text field. You can configure it however you need.
alert.addTextField { (textField) in
textField.text = ""
}
// 3. Grab the value from the text field, and print it when the user clicks OK.
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: { [weak alert] (_) in
let textField = alert?.textFields![0] // Force unwrapping because we know it exists.
print("An email has been sent to \(String(describing: textField?.text)) for account recovery")
}))
// 4. Present the alert.
self.present(alert, animated: true, completion: nil)
}
The input works fine as well the output although instead of giving another alert, it just prints what I need in the console.
Am I missing something?
I guess you misunderstood a bit: "print" is just printing on your console, if you want to open a new alert after touching on the "ok" button, you may want to complete your code with:
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: { [weak alert, weak self] (_) in
let message = "An email has been sent to \(alert?.textFields?.first?.text ?? "") for account recovery"
let innerAlert = UIAlertController(title: nil, message: message, preferredStyle: .alert)
innerAlert.addAction(UIAlertAction(title: "OK", style: .default, handler:nil))
self?.present(innerAlert, animated: true, completion: nil)
}))
I'm not sure what's wrong with this function. I'm trying to present an alert asking if the user would like to delete the selected photo.
If the function that deletes the photo returns an error, I would like to show that error to the user.
Xcode is failing on the errorController.addAction line with the message that "cannot call value of non-function type UIAlertAction"
I'm using Swift 2
#IBAction func deletePhoto(sender: AnyObject) {
let alertController = UIAlertController(title: "Delete Photo", message: "Are you sure you want to delete this photo?", preferredStyle: .Alert)
let okAction = UIAlertAction(title: "OK", style: .Default) { UIAlertAction in
self.photoGateway!.delete(self.photo!.id!, completion: { (withError: Bool) -> Void in
if (withError == true) {
let errorController = UIAlertController(title: "Delete Failed", message: "blah", preferredStyle: UIAlertControllerStyle.Alert)
// The next line is causing the error
errorController.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(errorController, animated: true, completion: nil)
} else {
self.dismissViewControllerAnimated(true, completion: nil)
}
})
}
let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel) { UIAlertAction in
print("Cancelled")
}
alertController.addAction(okAction)
alertController.addAction(cancelAction)
self.presentViewController(alertController, animated: true, completion: nil)
}
If I take out the offending line then all works well, just the user has no way of dismissing the alert
Fixed it. Changed:
errorController.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil))
to:
errorController.addAction(UIKit.UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil))
The reason this is happening is because the parameter for your callbacks is called UIAlertAction (lines 3 and 20 above) and this is overriding the declaration in UIKit. This is likely a mistake of code-completion. Just rename it to action or something like that or just _ as you don't reference it.
Very new to swift and coding in general and am trying to do a basic login/register page for my application. I'm following along with an online tutorial and (as far as i'm aware) done everything exactly the same. However, I am receiving two errors when attempting to build and I can't for the life of me figure it out! Any help would be great, thanks.
Image of all code for this page including errors
First error :
let userName = someUserNameLabel.text
let userDefaults = NSUserDefaults.standardUserDefaults()
userDefaults.setValue(userName, forKey: "userName")
userDefaults.synchronize() // don't forget this!!!!
Second error:
// Create the alert controller
var alertController = UIAlertController(title: "Alert", message: "Registration Successful", preferredStyle: .Alert)
// Create the actions
var okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default) {
UIAlertAction in
NSLog("OK Pressed")
}
// Add the actions
alertController.addAction(okAction)
// Present the controller
self.presentViewController(alertController, animated: true, completion: nil)
Second error: Swift 3 update
let alertController = UIAlertController(title: "Alert", message: "Registration Successful", preferredStyle: .alert)
let okAction = UIAlertAction(title: "OK", style: .default) {
print("OK Pressed")
}
alertController.addAction(okAction)
self.present(alertController, animated: true, completion: nil)
I am using the following function to display an alert with an "ok" and a "cancel" Button:
func displayModalDialog(#title: String, message: String, yesHandler: ((UIAlertAction!) -> Void)?, noHandler: ((UIAlertAction!) -> Void)?) {
var alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Default, handler: yesHandler))
alert.addAction(UIAlertAction(title: "Yes", style: UIAlertActionStyle.Default, handler: noHandler))
self.presentViewController(alert, animated: true, completion: nil)
}
This function is part of view controller which is the parent class of most view controllers in my app. When I start my app in the Simulator everything works fine and the alert is displayed with the specified parameters. However when I try to run it on a phone (4s with iOS7 and 5 with iOS8 tested) it crashes on the line:
var alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.Alert)
giving me a EXC_BAD_ACCESS error. Using exceptionBreakpoints did not reveal any additional information.
Hope anyone has help - thanks!