Create a swift empty project with the Xcode6-Beta, create a UIViewController, and select swift language.
@ IBOutlet var titleLabel: UILabel? @ IBOutlet var button: UIButton? // @ IBOutlet weak var button: UIButton? /@ IBAction func btnClick (AnyObject ){}
Drag a UIButton and UILabel in the xib file to bind the object. The operation is similar to that in Xcode.
Add the content of the click event. In this example, a UIAlertView is displayed, and the delegate is self. The delegate statement is followed by a comma, as shown below,
class MainViewController: UIViewController,UIAlertViewDelegate{}
The initialization code for the current successful test of UIAlertView is as follows:
let alert = UIAlertView() alert.title = "" alert.delegate = self alert.message = "Hello Apple" alert.addButtonWithTitle("OK") alert.addButtonWithTitle("Cancel") alert.show()
Other initialization APIs tried are executed abnormally.
// let alert = UIAlertView(title: "title", message: "Hello Apple", delegate: self, cancelButtonTitle:"Cancel")// let alert = UIAlertView(title: "title", message: "Hello Apple",delegate: self, cancelButtonTitle: "Cancel",otherButtonTitles:"OK");
Click the event delegate button to implement UIAlertView
func alertView(alertView: UIAlertView!, clickedButtonAtIndex buttonIndex: Int) { switch buttonIndex { case 0: println("alertView Ok!") case 1: println("alertView cancel!") default: println("alertView cancel!") } }
It can be seen that the switch does not require break, and the previous method cannot be used at present.
Case 0: case 1: println ("alertView cancel! ")
Break; // can be replaced with case 0 .. 2: println ("alertView OK! ")