Ios 警报控制器未出现错误!Swift项目
我正在尝试使用swift和firebase在iOS应用程序中更改(编辑)用户电子邮件。 在更改电子邮件功能时,我添加了一些错误,并针对每个错误发出警报 当我模拟并测试错误时,例如,我试图输入无效的电子邮件,错误工作正常! 但是警报没有出现,它给了我这个错误,如下图所示。 代码如下:Ios 警报控制器未出现错误!Swift项目,ios,xcode,swift,alert,uialertcontroller,Ios,Xcode,Swift,Alert,Uialertcontroller,我正在尝试使用swift和firebase在iOS应用程序中更改(编辑)用户电子邮件。 在更改电子邮件功能时,我添加了一些错误,并针对每个错误发出警报 当我模拟并测试错误时,例如,我试图输入无效的电子邮件,错误工作正常! 但是警报没有出现,它给了我这个错误,如下图所示。 代码如下: if EmailAddressTF.text=="" || PasswordTF.text=="" || BusinessNameTF.text=="" { let alert =
if EmailAddressTF.text=="" || PasswordTF.text=="" || BusinessNameTF.text==""
{
let alert = UIAlertController(title: "Oops!", message:"Make sure to fill in all required textfields", preferredStyle: .Alert)
alert.addAction(UIAlertAction(title: "OK", style: .Default) { _ in })
self.presentViewController(alert, animated: true){}
}
else {
if RepasswordTF.text != PasswordTF.text
{
let alert = UIAlertController(title: "Oops!", message:"You Entered Different Passwords", preferredStyle: .Alert)
alert.addAction(UIAlertAction(title: "OK", style: .Default) { _ in })
self.presentViewController(alert, animated: true){}
}
else {
let ref = Firebase(url: "https://businesswallet.firebaseio.com/")
let mainpass = NSUserDefaults.standardUserDefaults().objectForKey("password") as? String
let mainEmail = NSUserDefaults.standardUserDefaults().objectForKey("Email") as? String
let isEqu = (mainEmail == self.EmailAddressTF.text)
if(isEqu == true)
{ }
else
{
ref.changeEmailForUser(mainEmail, password: mainpass, toNewEmail: self.EmailAddressTF.text, withCompletionBlock: { error in
if error != nil {
if let errorCode = FAuthenticationError(rawValue: error.code) {
switch (errorCode) {
case .EmailTaken:
print("Email taken")
let alert = UIAlertController(title: "Oops!", message:"Sorry,Email taken", preferredStyle: .Alert)
alert.addAction(UIAlertAction(title: "OK", style: .Default) { _ in })
self.presentViewController(alert, animated:true){}
case .InvalidEmail:
print("invalid email")
let alert = UIAlertController(title: "Oops!", message:"invalid email", preferredStyle: .Alert)
alert.addAction(UIAlertAction(title: "OK", style: .Default) { _ in })
self.presentViewController(alert, animated:true){}
case .NetworkError:
print("Network Error")
let alert = UIAlertController(title: "Oops!", message:"Network error, check your connection", preferredStyle: .Alert)
alert.addAction(UIAlertAction(title: "OK", style: .Default) { _ in })
self.presentViewController(alert, animated: true){}
default:
print("Unknown Error")
let alert = UIAlertController(title: "Oops!", message:"Unknown Error", preferredStyle: .Alert)
alert.addAction(UIAlertAction(title: "OK", style: .Default) { _ in })
self.presentViewController(alert, animated: true){}
}
}
} else {
NSUserDefaults.standardUserDefaults().setValue(self.EmailAddressTF.text, forKey: "Email")
print("Email changed successfully")
}
})
} //Big Else
我该如何解决这个问题呢?这是obj-c代码,但您可以轻松地将其转换为swift
- (void)throwAlert
{
UIWindow* window = [[UIApplication sharedApplication] keyWindow];
UIAlertController *alert = [UIAlertController
alertControllerWithTitle:@"myTitle"
message:@"myMessage"
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel handler:^(UIAlertAction * action) {}];
[alert addAction:defaultAction];
dispatch_async(dispatch_get_main_queue(), ^{
if (window.rootViewController.presentedViewController != nil)
{
[window.rootViewController.presentedViewController presentViewController:alert animated:YES completion:nil];
}
else
{
[window.rootViewController presentViewController:alert animated:YES completion:nil];
}
});
}
斯威夫特:
func throwAlert()
{
let window = UIApplication.sharedApplication().keyWindow
let alert = UIAlertController(title: "Oops!", message:"You Entered Different Passwords", preferredStyle: .Alert)
alert.addAction(UIAlertAction(title: "OK", style: .Default) { _ in })
dispatch_async(dispatch_get_main_queue()) {
if window.rootViewController?.presentedViewController != nil
{
window.rootViewController?.presentedViewController!.presentViewController(alert, animated: true){}
}
else
{
window.rootViewController!.presentViewController(alert, animated: true){}
}
}
}
尝试在主线程中显示警报
dispatch_async(dispatch_get_main_queue()) {
let alertController = UIAlertController(title: head, message: message, preferredStyle: .Alert)
let OKAction = UIAlertAction(title: "OK", style: .Default) { (action:UIAlertAction!) in
print("you have pressed OK button");
}
alertController.addAction(OKAction)
self.presentViewController(alertController, animated: false, completion:nil)
}
要添加警报控制器,请在任何帮助器类中添加此全局方法
func popAlertMessageController(VC:(UIViewController), title:(String), message:(String)) {
let alertController:UIAlertController = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.Alert)
let ok:UIAlertAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default) { (action) -> Void in
alertController.dismissViewControllerAnimated(true, completion: nil)
}
alertController.addAction(ok)
VC.presentViewController(alertController, animated: true, completion: nil)
}
您可以在任何需要警报的地方调用此方法,如下所示:
popAlertMessageController(self, title: "Alert", message: "Your message")
这意味着您在错误的视图控制器或视图上显示该警报。听起来您试图从当前未显示的视图控制器显示警报控制器。但视图控制器当前已显示!!我不明白!!这意味着什么?您在另一个控制器上呈现一个视图控制器,但您没有保留对它的引用,因此它只会被情节提要保留,然后当您转到呈现另一个模式(也称为警报)时另一个视图不再在故事板中,由于没有引用而被取消分配,并且由于警报将在其上显示的控制器现在为零,因此它将失败。您也没有在主线程上显示警报。@A'sa Dickens,我现在应该怎么做才能解决这个问题?编译器强迫我在第二个点之前添加感叹号!当在模拟器中测试它时,它给了我以下错误:致命错误:在展开可选值时意外地发现了nil是的…您需要感叹号…这是正确的。但你的控制器显示为零,这就是崩溃发生的原因。你能把你的班级的代码贴在你试图调用此警报的地方吗?哦,请不要,我根本看不懂objective-C。你能用swift给我吗?让我们。让我们。应该在哪里准确地添加这个?在每个你想显示警报的地方。在你的情况下,在每一个
案例中
我都尝试过,它在控制台中给了我同样的错误!