ios - Assign Text in Code to UILabel! Swift 2.2 -
using tutorial ios 8/swift 1 create sample app in attempt assign text label, using xcode 7.3/swift 2.2. in advance.
import uikit class viewcontroller: uiviewcontroller { @iboutlet var agecat: uitextfield! @iboutlet var resultage: uilabel! @ibaction func findagebutton(sender: anyobject) { var enteredage = int(agecat.text!) var catyears = enteredage! * 7 resultage = "your cat \(catyears) in cat years" } }
replace ibaction func findagebutton following:
@ibaction func findagebutton(sender: anyobject) { // here variable unwrapped. means code after not executed unless program sure contains value. if let text = agecat.text { // can use unwrapped variable , won't have place exclamation mark behind force unwrap it. let enteredage = int(text) if let age = enteredage { let catyears = age * 7 resultage.text = "your cat \(catyears) in cat years" } } }
Comments
Post a Comment