ios - Swift iOS 如何从另一个警报消息的处理程序中显示警报消息

标签 ios swift handler alert message

我正在尝试在我的应用程序中实现忘记密码功能,以便用户可以输入他们的电子邮件并在数据库中重置它,并将新生成的密码通过电子邮件发送给他们。

我相信一旦我修复了一些小问题,我的代码就应该可以工作,但我遇到的主要障碍是如何从另一个警报消息的处理程序中显示警报消息。

知道该怎么做吗?要么警报消息根本不显示,要么第一条警报消息根本不关闭。

这是我的尝试:

    //This function will send a password reset email
func emailPassword(alertAction: UIAlertAction!) -> Void {
    let textField = reset_alert.textFields![0] as UITextField

    if(!textField.text!.isEmpty){

        if(textField != "mik"){
            let query = PFQuery(className: "_User")
            let forgotten_email = textField.text
            query.whereKey("email", equalTo: forgotten_email!)
            query.findObjectsInBackgroundWithBlock{
                (objects: [PFObject]?, error: NSError?) -> Void in

                //NO ERROR//
                if(error == nil){
                    //email in db generate random password
                    let letters : NSString = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"

                    let randomString : NSMutableString = NSMutableString(capacity: 12)
                    var users_name = ""

                    for _ in 0...10{
                        let length = UInt32 (letters.length)
                        let rand = arc4random_uniform(length)
                        randomString.appendFormat("%C", letters.characterAtIndex(Int(rand)))
                    }

                    //set new password for user
                    if let objects = objects {
                        for object in objects {
                            object["_hashed_password"] = randomString
                            users_name = object["name"] as! String

                            //send password to email
                            self.mailgun.sendMessageTo("\(users_name) <\(textField)>", from: "")

                            self.displayAlert("SUCCESS", msg: "Check your email for your new password")
                            self.reset_alert.dismissViewControllerAnimated(true, completion: nil)
                        }
                    }
                    else{
                        self.reset_alert.dismissViewControllerAnimated(true, completion: nil)
                        self.displayAlert("ERROR", msg: "Email not registered to an account")

                    }
                }

                    //ERROR//
                else{
                    self.reset_alert.dismissViewControllerAnimated(true, completion: nil)
                    self.displayAlert("ERROR", msg: "Email not registered to an account")                    }
            } //end if textfield not admin email

            self.presentViewController(reset_alert, animated: true, completion: nil)
        }
    }//end of if textfield is empty
}

最佳答案

尝试这个扩展:

typealias AlertActionBlock = (UIAlertAction) -> Void
extension UIViewController {
    func flash(title title:String?, message:String?, cancelTitle:String?, actions:UIAlertAction?...) {
        let b = {
            let alertController = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.Alert)
            let cancelBlock:AlertActionBlock = {(action:UIAlertAction) -> Void in }
            let cancelAction = UIAlertAction(title: cancelTitle, style: UIAlertActionStyle.Cancel, handler: cancelBlock)
            alertController.addAction(cancelAction)
            for action in actions {if let action = action {alertController.addAction(action)}}
            self.presentViewController(alertController, animated: true, completion: nil)
        }
        if NSThread.isMainThread() {
            b()
        } else {
            dispatch_async(dispatch_get_main_queue(), b)
        }
    }
}

这应该让您从后台线程调用该函数,或者主线程只需调用该函数并填充变量

编辑(我没有意识到你需要一个文本字段)所以试试这个:

func flash(title title:String?, message:String?, textFieldConfigurator:(UITextField -> Void)? = nil, cancelTitle:String?, actions:UIAlertAction?...) {
        let b = { () -> Void in
            let alertController = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.Alert)
            if let textFieldConfigurator = textFieldConfigurator {alertController.addTextFieldWithConfigurationHandler(textFieldConfigurator)}
            let cancelBlock:AlertActionBlock = {(action:UIAlertAction) -> Void in }
            let cancelAction = UIAlertAction(title: cancelTitle, style: UIAlertActionStyle.Cancel, handler: cancelBlock)
            alertController.addAction(cancelAction)
            for action in actions {if let action = action {alertController.addAction(action)}}
            self.presentViewController(alertController, animated: true, completion: nil)
        }
        if NSThread.isMainThread() {
            b()
        } else {
            dispatch_async(dispatch_get_main_queue(), b)
        }
    }

如果您需要多个文本字段,请将其作为数组并迭代它。让我知道进展如何!

关于ios - Swift iOS 如何从另一个警报消息的处理程序中显示警报消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37890427/

相关文章:

java - spring-oauth2 登录成功处理器

android - 使用 handler.postDelayed 获取剩余时间

ios - objective-c ,聊天消息应用程序和识别时区

ios - 如何将推送通知 token 存储到外部数据库中 - iOS 10、Swift 3

ios - 如何在标签上显示数组

javascript - 我想将 JSON 数据传递给与 web View 相关的 javascript 函数。但是我无法从 Javascript 函数中获取响应数据

jquery - AngularJS/jQuery - 如何在 jQuery 事件处理程序中附加 $scope 对象的一些属性

ios - 在 ios 中使用 GCM 一次向多个主题发送推送通知 #ios # google-cloud-messaging

ios - 如何继承 NSException 并以更好的方式使用?

swift - 物理体 : Could not create physics body