Hi i'm making a register tableview and i have to show the total of mistakes that the person did filling it in uialertcontroller.
for var i = 0; i < subJson.count; i++ {
let messag = ("\(key): \(subJson[i])")
let alert = UIAlertController(title: "Error al registrarse", message: messag, preferredStyle: .Alert)
let acceptAction = UIAlertAction(title: "Aceptar", style: .Default , handler: nil)
alert.addAction(acceptAction)
self.presentViewController(alert, animated: true, completion: nil)
}
I have this and the error is it :
Attempt to present UIAlertController on which is already presenting (null)
No you can't, you can only show one alert at a time.
It's not possible now because de uialertview it's deprecate in xcode version 7.0.1.
So try to rebuild your application logic
Related
I want to have an alert action change the text of a UILabel in my View. Code below:
#IBAction func statusChange(_ sender: UIButton){
let playalert = UIAlertController(title: "OPERATION: \(sender.accessibilityIdentifier!) NOT POSSIBLE", message: "Convert op to PLAY mode to proceed", preferredStyle: .alert)
let recordalert = UIAlertController(title: "OPERATION: \(sender.accessibilityIdentifier!) NOT POSSIBLE", message: "Convert op to STOP mode to proceed", preferredStyle: .alert)
let statechangealert = UIAlertController(title: "OPERATION: \(sender.accessibilityIdentifier!) WILL NOW PROCEED", message: "", preferredStyle: .alert)
let stopAction = UIAlertAction(title: "Convert", style: .default, handler: { action in
self.statusDVR.text = "STOP"//"\(sender.accessibilityIdentifier!)"
})
let continueAction = UIAlertAction(title: "Continue", style: .default, handler: { action in
self.statusDVR.text = ""//"\(sender.accessibilityIdentifier!)"
})
let returnAction = UIAlertAction(title: "Return", style: .default, handler: { action -> Void in
})
playalert.addAction(stopAction)
playalert.addAction(returnAction)
recordalert.addAction(stopAction)
recordalert.addAction(returnAction)
statechangealert.addAction(continueAction)
//record was tapped, check if DVR stopped first
if (sender.tag == 5 && pwrStat == true ) {
//if not stopped send alert
if statusDVR.text != "STOP" {
self.present(recordalert, animated:true, completion: nil)
//if user tapped returned action
if statusDVR.text != "STOP" {
self.present(statechangealert, animated:true, completion: nil)
} else {
return
}
} else {
statusDVR.text = "RECORD"
}
The stopAction should convert a UILAbel called statusDVR in the view controller, but it doesn't do it.
I haven't tried implementing the other Actions yet, because I am stuck trying to figure out why my UILabel's text won't change. Thank you for any help :)
First of all, this code makes no sense:
if statusDVR.text != "STOP" {
self.present(recordalert, animated:true, completion: nil)
if statusDVR.text != "STOP" {
self.present(statechangealert, animated:true, completion: nil)
That code tries to present two alerts at the same time. That is illegal.
I think you think that when you say
self.present(recordalert, animated:true, completion: nil)
...your code magically comes to a stop while the user interacts with the alert and then proceeds after the user dismisses the alert. That’s not the case. Your code never magically stops; it just keeps right on going.
As for the actual question you asked about, the problem is simply that what you're doing is illegal:
playalert.addAction(stopAction)
playalert.addAction(returnAction)
recordalert.addAction(stopAction)
recordalert.addAction(returnAction)
No! You cannot take one UIAlertAction and somehow magically "share" it between two different UIAlertControllers. Every UIAlertController needs UIActions that belong to it alone. In other words, do not call addAction on the same UIAlertAction twice.
Just to demonstrate more simply, try running this code:
let action1 = UIAlertAction(title: "Test", style: .default) {
_ in print("test")
}
let alert1 = UIAlertController(title: "Hello", message: nil, preferredStyle: .alert)
let alert2 = UIAlertController(title: "Hello2", message: nil, preferredStyle: .alert)
alert1.addAction(action1)
alert2.addAction(action1)
self.present(alert1, animated: true, completion: nil)
The alert appears, you tap the Test button, the alert disappears — but nothing prints in the console. Now comment out the next to last line:
// alert2.addAction(action1)
... and run the code again. This time, when you tap Test, "test" appears in the console.
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 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)
}
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 have just released my app on the App Store however I have just been emailed and told that there is issues with crashing.
The issue is with Alert Views which crash my app when they are supposed to appear (only in iOS 7). I had some code in place that was supposed to fix this issue, however it doesn't seem to work in certain versions of iOS 7.
Here is my current code:
#IBAction func resetAllButton(sender : AnyObject) {
//If statement to check whether the modern style alert is available. Prior to iOS 8 it is not.
if let gotModernAlert: AnyClass = NSClassFromString("UIAlertController") {
println("UIAlertController can be instantiated")
var alert = UIAlertController(title: "Start Over", message: "Are you sure you want to start over? This will erase your budget and all transactions.", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "I'm sure!", style: UIAlertActionStyle.Default, handler:{ (ACTION :UIAlertAction!)in
self.resetView()
}))
alert.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
}
else {
println("UIAlertController can NOT be instantiated")
var alertView = UIAlertView()
alertView.delegate = self
alertView.title = "Start Over"
alertView.message = "Are you sure you want to start over? This will erase your budget and all transactions."
alertView.addButtonWithTitle("I'm sure!")
alertView.addButtonWithTitle("Cancel")
alertView.show()
}
}
What can I do to ensure that my app doesn't crash on any version of iOS 7 or 8?
I had the same problem in the relese build.
It seems an internal bug of the swift compiler (using Xcode 6.0.1 (6A317) )
I solved actually with a objC helper class with this method:
+ (BOOL) checkIfClassExists:(NSString *)className {
id c = objc_getClass([className cStringUsingEncoding:NSASCIIStringEncoding]);
if (c != nil) {
return YES;
} else {
return NO;
}
}
called in my swift code with a bridge header
if ObjCFix.checkIfClassExists("UIAlertController") {
//iOS 8 code here
} else {
//iOS 7 code here
}
Here is my drag and drop swift solution:
//Alerts change in iOS8, this method is to cover iOS7 devices
func CozAlert(title: String, message: String, action: String, sender: UIViewController){
if respondsToSelector("UIAlertController"){
var alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: action, style: UIAlertActionStyle.Default, handler:nil))
sender.presentViewController(alert, animated: true, completion: nil)
}
else {
var alert = UIAlertView(title: title, message: message, delegate: sender, cancelButtonTitle:action)
alert.show()
}
}
Call like this:
CozAlert("reportTitle", message: "reportText", action: "reportButton", sender: self)
Beware this is only for the most basic alerts, you might need additional code for advanced stuff.