Swift的UIAlertController用法
// 定义button
let centerBtn = UIButton(frame:CGRectMake(view.frame.size.width/2-50,100,100,50))
centerBtn.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
centerBtn.setTitle("中部弹出框", forState: UIControlState.Normal)
centerBtn.backgroundColor = UIColor.orangeColor()
centerBtn.addTarget(self, action: #selector(ViewController.centerBtnClicked), forControlEvents: UIControlEvents.TouchUpInside)
self.view.addSubview(centerBtn)
// 定义button
let bottomBtn = UIButton(frame:CGRectMake(view.frame.size.width/2-50,200,100,50))
bottomBtn.backgroundColor = UIColor.brownColor()
bottomBtn.setTitle("底部弹出框", forState: UIControlState.Normal)
bottomBtn.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
bottomBtn.addTarget(self, action: #selector(ViewController.bottomBtnClicked), forControlEvents: UIControlEvents.TouchUpInside)
self.view.addSubview(bottomBtn)
}
// 底部弹出框
func bottomBtnClicked(){
let alertSheet = UIAlertController.init(title: "分享", message: nil,preferredStyle: .ActionSheet)
let alertSheetAction = UIAlertAction.init(title: "朋友圈分享",style: .Default,handler: {(UIAlertAction) ->Void in
print("朋友圈分享")
})
let alertSheetDestructive = UIAlertAction.init(title: "QQ分享",style: .Default,handler: {(UIAlertAction) ->Void in
print("QQ分享")
})
let alertSheetCancel = UIAlertAction.init(title: "取消", style: .Cancel,handler: {(UIAlertAction) ->Void in
print("取消")
})
alertSheet.addAction(alertSheetAction)
alertSheet.addAction(alertSheetDestructive)
alertSheet.addAction(alertSheetCancel)
self.presentViewController(alertSheet, animated: true, completion: nil)
}
//中部弹出框
func centerBtnClicked(){
// 中间弹出效果
let alertView: UIAlertController = UIAlertController.init(title: "提示", message: "是否要更新", preferredStyle: UIAlertControllerStyle.Alert)
let alertViewDefaultAction: UIAlertAction = UIAlertAction.init(title: "确定", style: UIAlertActionStyle.Default, handler: { (UIAlertAction) -> Void in
print("正在更新")
})
let alertViewCanceltAction: UIAlertAction = UIAlertAction.init(title: "取消", style: UIAlertActionStyle.Cancel, handler: { (UIAlertAction) -> Void in
print("取消更新")
})
// 红色的字体
let alertViewDestructiveAction: UIAlertAction = UIAlertAction.init(title: "销毁",style: UIAlertActionStyle.Destructive, handler: {(UIAlertAction) -> Void in
print("已经销毁掉了")
} )
// 用户名
alertView.addTextFieldWithConfigurationHandler({(UITextField) -> Void in
UITextField.placeholder = "name"
UITextField.clearButtonMode = UITextFieldViewMode.WhileEditing
})
// 密码
alertView.addTextFieldWithConfigurationHandler({(UITextField) -> Void in
UITextField.placeholder = "password"
UITextField.clearButtonMode = UITextFieldViewMode.WhileEditing
})
alertView.addAction(alertViewDefaultAction)
alertView.addAction(alertViewCanceltAction)
alertView.addAction(alertViewDestructiveAction)
self.presentViewController(alertView, animated:true , completion: nil)
}