1, the following code creates and pops up an alarm box with a "Cancel" "OK" button of two
(note: Since IOS8, we recommend using Uialertcontroller)
| 123456789101112131415161718192021222324 |
class ViewController: UIViewController{ override func viewDidLoad() { super.viewDidLoad() var alertView = UIAlertView() alertView.title = "系统提示" alertView.message = "您确定要离开hangge.com吗?" alertView.addButtonWithTitle("取消") alertView.addButtonWithTitle("确定") alertView.cancelButtonIndex=0 alertView.delegate=self; alertView.show() } func alertView(alertView:UIAlertView, clickedButtonAtIndex buttonIndex: Int){ if(buttonIndex==alertView.cancelButtonIndex){ println("点击了取消") } else { println("点击了确认") } }} |
2, the Alarm box has the following 4 kinds of styles
Default: Defaults style
Plaintextinput: Alarm box with input box
Securetextinput: Alarm box with Password box
Loginandpasswordinput: Alarm box with input box and password box
Here is an example of an alarm box using an input box and a password box:
| 1234567891011121314151617181920212223242526272829303132333435 |
import UIKitclass ViewController: UIViewController { var alertView = UIAlertView() override func viewDidLoad() { super.viewDidLoad() alertView.title = "系统登录" alertView.message = "请输入用户名和密码!" alertView.addButtonWithTitle("取消") alertView.addButtonWithTitle("确定") alertView.cancelButtonIndex=0 alertView.delegate=self; alertView.alertViewStyle = UIAlertViewStyle.LoginAndPasswordInput alertView.show() } func alertView(alertView:UIAlertView, clickedButtonAtIndex buttonIndex: Int){ if(buttonIndex==alertView.cancelButtonIndex){ println("点击了取消") } else { let name = alertView.textFieldAtIndex(0) let password = alertView.textFieldAtIndex(1) println("用户名是:\(name!.text) 密码是:\(password!.text)") } } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() }} |
Use of Swift-alarm box (Uialertview)