您的当前位置:首页正文

Swift里的AlertController即OC里的AlerV

来源:花图问答
    let alertController = UIAlertController(title: "New Resolution",                
message: "", preferredStyle: UIAlertControllerStyle.Alert)
    //confirmAction
    let confirmAction = UIAlertAction(title: "Confirm", style:   
UIAlertActionStyle.Default, handler: ({
        (_) in
        if let field = alertController.textFields![0] as UITextField!{
            self.saveItem(field.text!)
            self.tableView.reloadData()    
        }
    }))
    
    let cancelAction = UIAlertAction(title: "Cancel", style:      
UIAlertActionStyle.Cancel, handler: nil)

    alertController.addTextFieldWithConfigurationHandler ({
        (textField) in
        textField.placeholder = "Type smothing..."
    })
    
    alertController.addAction(confirmAction)
    alertController.addAction(cancelAction)

    self.presentViewController(alertController, animated: true, 
completion: nil)