Friday, 26 February 2016

UIAlertController in Swift

A UIAlertController object displays an alert message. This class replaces the UIActionSheet and UIAlertView classes.
In addition to displaying a message, associate actions with alert controller to give a way to respond.
Click Here for more details.

//Display UIAlertView
func displayAlert(){
        let alertController = UIAlertController(title: "Alert Message", message: "Welcome to you", preferredStyle: .Alert)
        alertController.addAction(UIAlertAction(title: "Cancel", style: .Cancel, handler: nil))
        presentViewController(alertController, animated: true, completion: nil)
    }


//Add More action with UIAlertController
func alertOkButtonAction(actionTarget: UIAlertAction){
        print("Ok Action Call")
    }

 //create a action
let OkAction = UIAlertAction(title: "Ok", style: .Default, handler: alertOkButtonAction)
alertController.addAction(OkAction)


//Display alert with  UITextField
alertController.addTextFieldWithConfigurationHandler { (nameTxtFld) -> Void in
           nameTxtFld.textColor = UIColor.blueColor()
        }

//We can also add action on UItextField
 func textChange(txtFld:UITextField){
        let str = txtFld.text! as NSString
        print(str)
       
    }

//Add action on UITextField
alertController.addTextFieldWithConfigurationHandler { (nameTxtFld) -> Void in
           nameTxtFld.textColor = UIColor.blueColor()

           nameTxtFld.addTarget(self, action: "textChange:", forControlEvents: .EditingChanged)
        }

No comments:

Post a Comment