前端之家收集整理的这篇文章主要介绍了
Swift UIAlterViewController,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
//定义按钮,显示最简单的Alter
let button1 = UIButton(type: UIButtonType.System)
button1.frame = CGRectMake(30,70,self.view.frame.size.width-60,35)
button1.setTitle("最简单的Alter",forState: UIControlState.Normal)
button1.addTarget(self,action: "button1Action:",forControlEvents: UIControlEvents.TouchUpInside)
self.view .addSubview(button1)
//定义按钮,显示带文本框的Alter
let button2 = UIButton(type: UIButtonType.System)
button2.frame = CGRectMake(30,125,35)
button2.setTitle("带文本框的Alter",forState: UIControlState.Normal)
button2.addTarget(self,action: "button2Action:",forControlEvents: UIControlEvents.TouchUpInside)
self.view.addSubview(button2)
//定义按钮,显示上拉菜单
let button3 = UIButton(type: UIButtonType.System)
button3.frame = CGRectMake(30,180,35)
button3.setTitle("上拉菜单",forState: UIControlState.Normal)
button3.addTarget(self,action: "button3Action:",forControlEvents: UIControlEvents.TouchUpInside)
self.view.addSubview(button3)
}
func button1Action(sender :UIButton) {
let alterController = UIAlertController(title: "最简单的Alter",message: nil,preferredStyle: UIAlertControllerStyle.Alert)
let action1 = UIAlertAction(title: "星期一",style: UIAlertActionStyle.Default) { (UIAlertAction) -> Void in
}
let action2 = UIAlertAction(title: "星期二",style: UIAlertActionStyle.Destructive) { (UIAlertAction) -> Void in
}
let action3 = UIAlertAction(title: "星期三",style: UIAlertActionStyle.Default) { (UIAlertAction) -> Void in
}
let action4 = UIAlertAction(title: "取消",style: UIAlertActionStyle.Cancel) { (UIAlertAction) -> Void in
}
alterController.addAction(action1)
alterController.addAction(action2)
alterController.addAction(action3)
alterController.addAction(action4)
self .presentViewController(alterController,animated: true,completion: nil)
}
func button2Action(sender :UIButton) {
let alterController = UIAlertController(title: "带文本框的Alter",preferredStyle: UIAlertControllerStyle.Alert)
alterController.addTextFieldWithConfigurationHandler { (textField: UITextField) -> Void in
textField.placeholder = "请输入账号"
}
alterController.addTextFieldWithConfigurationHandler { (textField: UITextField) -> Void in
textField.placeholder = "请输入密码"
textField.secureTextEntry = true
}
let action = UIAlertAction(title: "登录",style: UIAlertActionStyle.Cancel) { (UIAlertAction) -> Void in
}
alterController.addAction(action)
self.presentViewController(alterController,animated: true) { () -> Void in
}
}
func button3Action(sender: UIButton) {
let alterController = UIAlertController(title: "上拉菜单",preferredStyle: UIAlertControllerStyle.ActionSheet)
let action1 = UIAlertAction(title: "星期一",completion: nil)
}
}
DEMO下载