This simple app is written in swfit1.2, the main focus is: 1. Click the Keyboard return button to exit the keyboard. 2. Delivery of individual control values.
Storyboard Interface Design:
View Code:
1 Import UIKit2 3 class Viewcontroller:uiviewcontroller, uitextfielddelegate{4 5 @IBOutlet weak var name:uitextfield!6 @IBOutlet weak var gender:uisegmentedcontrol!7 @IBOutlet weak var birthday:uidatepicker!8 @IBOutlet weak var heightnumber:uislider!9 @IBOutlet weak var height:uilabel!Ten @IBOutlet weak var hasproperty:uiswitch! One @IBOutlet weak var result:uitextview! A - override func Viewdidload () { - super.viewdidload () the name.delegate = self//Click Return to exit the keyboard - } - override func didreceivememorywarning () { - super.didreceivememorywarning () + //Dispose of any resources, can be recreated. - } + A @IBAction func heightchanged (sender:anyobject) { at var slider = sender as! UISlider - var i = int (slider.value)//float converted to int - Slider.value = Float (i)//conversion Operation - height.text = "\ (i) cm"//change the value of the label with the slider - } - in @IBAction func oktapped (sender:anyobject) { - Let gendertext = gender.selectedsegmentindex==0? "Gao Fu Shuai": "White rich beauty" to Let Gregorian = Nscalendar (Calendaridentifier:nscalendaridentifiergregorian) + Let now = NSDate () - birthday.maximumdate = NSDate ()//To prevent negative age the Let components = Gregorian?. Components (Nscalendarunit.calendarunityear, FromDate:birthday.date, Todate:now, options:nscalendaroptions (0))// Calculate Age * $ Let -age = components!. year//Get AgePanax Notoginseng Let haspropertytext = Hasproperty.on? "Room": "No Room"//switch Value data - the result.text = "\ (name.text), \ (age), \ (gendertext), height \ (height.text!), \ (haspropertytext), Seek intercourse!" + A } the + //uitextfileddelegate, click Return to make the keyboard exit - func Textfieldshouldreturn (Textfield:uitextfield), bool{ $ Textfield.resignfirstresponder () $ return True - } -}
It is important to note in this code that, because of the swift language change, it needs to be added after the assignment or display of age and Height.text! , or you will get an error.
Operating effect:
Dating Artifact Record Swift1.2