Using the UIAlertController in Swift (iOS8.1)

Swift (and objective C) comes with a handy way to handle login, confirmations, etc in alerts.
Here is an example


func handleParseLogin(){
        
        let alertController = UIAlertController(title: "Login!", message: "Please log in to the app", preferredStyle: .Alert)
        let loginAction = UIAlertAction(title: "Login", style: .Default) { (_) in
            let loginTextField = alertController.textFields![0] as UITextField
            let passwordTextField = alertController.textFields![1] as UITextField
            self.login(loginTextField.text, passwd: passwordTextField.text)
        }
        loginAction.enabled = false
        
        //let forgotPasswordAction = UIAlertAction(title: "Forgot Password", style: .Destructive) { (_) in }
        let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel) { (_) in }
        
        alertController.addTextFieldWithConfigurationHandler { (textField) in
            textField.placeholder = "Login"
            
            NSNotificationCenter.defaultCenter().addObserverForName(UITextFieldTextDidChangeNotification,
                object: textField, queue: NSOperationQueue.mainQueue()) { (notification) in
                loginAction.enabled = textField.text != ""
            }
        }
        
        alertController.addTextFieldWithConfigurationHandler { (textField) in
            textField.placeholder = "Password"
            textField.secureTextEntry = true
        }
        
        alertController.addAction(loginAction)
        //alertController.addAction(forgotPasswordAction)
        alertController.addAction(cancelAction)
        self.presentViewController(alertController, animated: true, completion: nil)
        

    }
   
Here is how it look, I am sure there are plenty of ways to make it better looking

iOS Simulator Screen Shot 04 Nov 2014 17.52.09

By the way, this is a log in to Parse, go and have a look, it seems to be great.
Posted in Swift Programming