// 提示框有 UIAlertView(iOS8前) 和 UIAlertController(iOS8起)
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
/// alertView 不能直接放在viewDidLoad()方法中,放在里面会爆 “Attempt to present <UIAlertController: 0x7f8e59d65550> on <Swift_UIAlertView.ViewController: 0x7f8e59f1a4d0> whose view is not in the window hierarchy!” 的错误,这是因为视图还没加载出来,就用presentView,而出现的
let button = UIButton(type:UIButtonType.Custom)
button.frame = CGRectMake(0, 20, 60, 60)
button.backgroundColor = UIColor.blackColor()
button.setTitle("弹框", forState: .Normal)
button.addTarget(self, action: #selector(buttonAction(_:)), forControlEvents: UIControlEvents.TouchUpInside)
self.view.addSubview(button)
let button1 = UIButton(type:UIButtonType.Custom)
button1.frame = CGRectMake(0, 100, 60, 60)
button1.backgroundColor = UIColor.blackColor()
button1.setTitle("弹框1", forState: .Normal)
button1.addTarget(self, action: #selector(buttonAction1(_:)), forControlEvents: UIControlEvents.TouchUpInside)
self.view.addSubview(button1)
}
//
func buttonAction(sender:UIButton) {
/**
1,下面代码创建并弹出一个告警框,并带有“取消”“确定”两个按钮
UIAlertControllerStyle:
ActionSheet 从屏幕底部弹出并显示,点击alertView 外部 隐藏提示框
Alert 在屏幕中部显示,点击alertView 外部无效果
*/
let alertView = UIAlertController(title: "系统提示",message: "我是美女吗?", preferredStyle: UIAlertControllerStyle.ActionSheet)
/**
2,UIAlertAction:
cancel 那么字体会变粗;
destructive,字体会显示红色。
*/
// 传参
let alertAction1 = UIAlertAction(title: "是的", style: UIAlertActionStyle.Default, handler:{(alertAction:UIAlertAction)->()in
print("我是大美女")
})
let alertAction2 = UIAlertAction(title: "beautiful girl!", style: UIAlertActionStyle.Destructive, handler: {
(alerts: UIAlertAction!)->Void in
print("倾国倾城")
})
// 不传参的情况下
let alertAction3 = UIAlertAction(title: "的确是", style: UIAlertActionStyle.Cancel, handler: { _ in
print("取消")
})
// action 添加到alertView 中
alertView.addAction(alertAction1)
alertView.addAction(alertAction2)
alertView.addAction(alertAction3)
self.presentViewController(alertView, animated: true, completion: nil)
}
func buttonAction1 (button:UIButton){
let alertView = UIAlertController(title: "", message: "一起去旅游?", preferredStyle: UIAlertControllerStyle.Alert)
let alertAction1 = UIAlertAction(title: "好的",style: .Destructive, handler: {
action in switch action.style{
case .Default:
print("OK")
case .Cancel:
print("cancel")
case .Destructive:
print("Destructive")
// 取出文本内容
let textfield:UITextField = (alertView.textFields?.first)!
print(textfield.text)
}
})
let alertAction2 = UIAlertAction(title: "取消", style: .Cancel, handler: {_ in
print("cancel")
})
// 添加输入框
alertView.addTextFieldWithConfigurationHandler { (textField :UITextField) in
textField.placeholder = "输入地名"
textField.secureTextEntry = true
}
alertView.addAction(alertAction1)
alertView.addAction(alertAction2)
self.presentViewController(alertView, animated: true, completion: nil);
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Swift 提示框 UIAlertViewController
最新推荐文章于 2020-07-23 10:00:10 发布