警报控制器不会出现错误!斯威夫特项目

时间:2016-04-15 18:07:29

标签: ios xcode swift alert uialertcontroller

我正在尝试使用swift和firebase在iOS应用程序中更改(编辑)用户电子邮件。 在更改电子邮件功能时,我添加了一些错误并警告每个错误。

当我模拟并测试错误时,例如,我尝试输入无效的电子邮件,错误正常! 但警报没有出现,它给了我这个错误,如下图所示。 When a DependsOn attribute is required

以下是代码:

function twitchInfo(user, callback){
    $.ajax({
      url: streams + user,
      success: callback
    })
};

我该如何解决这个问题?

3 个答案:

答案 0 :(得分:0)

这是对象代码,但您可以轻松将其转换为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){}
            }
        }

    }

答案 1 :(得分:0)

尝试在主线程中显示提醒

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)
}

答案 2 :(得分:0)

要添加警报控制器,请在任何帮助程序类中添加此全局方法。

(people + 1)->name; /* the same as people[1].name */

您可以从警报所需的任何地方调用此方法,如下所示:

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)
}