// Viewcontroller.swift Tanchu // Created by Fang Yunqi on 15/12/1. Copyright (c) 2015 JS. All rights reserved. //
Import Uikit
Class Viewcontroller:uiviewcontroller { var alert1:uialertcontroller! var alert2:uialertcontroller! var actionsheet:uialertcontroller! Override Func Viewdidload () { Super.viewdidload () Do no additional setup after loading the view, typically from a nib. Define a button that shows the simplest Alert Let button1 = Uibutton.buttonwithtype (. System) as! UIButton Button1.frame = CGRectMake (SELF.VIEW.FRAME.WIDTH/2-200, 50, 400, 50) Button1.settitle ("The simplest Alert", forState:UIControlState.Normal) Button1.addtarget (Self, Action: "Buttonaction:", ForControlEvents:UIControlEvents.TouchUpInside) Button1.tag = 1
Defines a button that displays Alert with a text box Let button2 = Uibutton.buttonwithtype (. System) as! UIButton Button2.frame = CGRectMake (SELF.VIEW.FRAME.WIDTH/2-200, 150, 400, 50) Button2.settitle ("Alert with text box", ForState:UIControlState.Normal) Button2.addtarget (Self, Action: "Buttonaction:", ForControlEvents:UIControlEvents.TouchUpInside) Button2.tag = 2
Define a button to display the Pull menu Let Button3 = Uibutton.buttonwithtype (. System) as! UIButton Button3.frame = CGRectMake (SELF.VIEW.FRAME.WIDTH/2-200, 250, 400, 50) Button3.settitle ("Pull menu", ForState:UIControlState.Normal) Button3.addtarget (Self, Action: "Buttonaction:", ForControlEvents:UIControlEvents.TouchUpInside) Button3.tag = 3
Define Cancel, OK, save, delete, reset uialertaction var cancelaction = uialertaction (title: "Cancel", Style:UIAlertActionStyle.Cancel, Handler:nil) var okaction = uialertaction (title: "OK", Style:UIAlertActionStyle.Default) { (action:uialertaction!) -> Void in println ("You choose OK") } var saveaction = uialertaction (title: "Save", Style:UIAlertActionStyle.Default) { (action:uialertaction!) -> Void in println ("You choose Save") } var deleteaction = uialertaction (title: "Delete", style:UIAlertActionStyle.Destructive) { (action:uialertaction!) -> Void in println ("You choose Delete") } var resetaction = uialertaction (title: "Reset", style:UIAlertActionStyle.Destructive) { (action:uialertaction!) -> Void in println ("You choose Reset") }
1, initialization of the simplest Alert Alert1 = Uialertcontroller (title: ' Simple Alert ', message: ' This is ' simple alert ', preferredstyle:uialertcontrollerstyl E.alert) Alert1.addaction (cancelaction) Alert1.addaction (resetaction) Alert1.addaction (okaction)
2. Initialize Alert with text box Alert2 = Uialertcontroller (title: "Login Alert", Message: "Please enter your name and password", Preferredstyle:uialertco Ntrollerstyle.alert) Alert2.addtextfieldwithconfigurationhandler { (textfield:uitextfield!) -> Void in Textfield.placeholder = "Name" } Alert2.addtextfieldwithconfigurationhandler { (textfield:uitextfield!) -> Void in Textfield.placeholder = "Password" Textfield.securetextentry = True } var loginaction = uialertaction (title: "Login", Style:UIAlertActionStyle.Default) { (action:uialertaction!) -> Void in var name = self.alert2.textfields!. The as! Uitextfield var password = self.alert2.textfields!. Last as! Uitextfield println ("Name: \" (Name.text); Password: \ (password.text) ") } Alert2.addaction (loginaction)
3. Initialize the menu of Pull up Actionsheet = Uialertcontroller (title: ' Simple action sheet ', message: ' Action sheet message ', Preferredstyle:uialertcon Trollerstyle.actionsheet) Actionsheet.addaction (cancelaction) Actionsheet.addaction (deleteaction) Actionsheet.addaction (saveaction)
Self.view.addSubview (button1) Self.view.addSubview (Button2) Self.view.addSubview (Button3) Self.view.addSubview (Button3) }
Button Response Event Func buttonaction (Sender:uibutton) { Let num = Sender.tag Switch num { Case 1: Self.presentviewcontroller (Alert1, Animated:true, Completion:nil) Case 2: Self.presentviewcontroller (Alert2, Animated:true, Completion:nil) Case 3: Self.presentviewcontroller (Actionsheet, Animated:true, Completion:nil) Default Break } }
Override Func didreceivememorywarning () { Super.didreceivememorywarning () Dispose of any of the can is recreated. }
} |