Sunday, 10 December 2017

Segment Controls

1) Switch : 
step-1 : Put Switch Control and UITextfield on ViewController from object Library.
step-2 : Give meaningful Constraints on Switch and UITextfield.
step-3 : Give the IBoutlet of switch and UITextfield.
step-4 : Write following code in ViewDidLoad.


var icon_1 : Bool!
override func viewDidLoad() {
        super.viewDidLoad()
        switch_1.addTarget(self, action: #selector(stateChange), for: .valueChanged)
        icon_1 = true
        // Do any additional setup after loading the view, typically from a nib.

    }



Here, 'switch_1' is IBoutlet of switch, 'icon_1' is boolean variable and 'stateChange' is function.

step-5 : In stateChange function,

func stateChange(sender: UISwitch) {
        
        if icon_1 == true {
            txt_value.isSecureTextEntry = false
            icon_1 = false
        }
        else {
            txt_value.isSecureTextEntry = true
            icon_1 = true
        }
    }


Here, 'txt_value' is IBoutlet of UITextfield.



2) Segment Control : 

step-1 : Put Segment Control on ViewController from object Library.
step-2 : Give meaningful Constraints on Segment Control.
step-3 : Give IBoutlet and IBAction of Segment Control.
step-4 : Put following code in IBAction of Segment Control.



@IBAction func seg_click(_ sender: UISegmentedControl) {
        print(sender.selectedSegmentIndex)

    }



3) Slider :

step-1 : Put Slider on ViewController from object Library.
step-2 : Give meaningful Constraints on Slider.
step-3 : Give IBoutlet and IBAction of Slider.
step-4 : Put following code in IBAction of Slider.



@IBAction func slider_click(_ sender: UISlider) {
        let currentValue = Int(sender.value)
        lbl_display.text = "\(currentValue)"

    }

Here, 'lbl_display' is IBoutlet of UILabel.


4) Stepper :

step-1 : Put Stepper on ViewController from object Library.
step-2 : Give meaningful Constraints on Stepper.
step-3 : Give IBoutlet and IBAction of Stepper.
step-4 : Put following code in IBAction of Stepper.



@IBAction func stepper_click(_ sender: UIStepper) {
        lbl_stepper.text = Int(sender.value).description

    }

here,  lbl_stepper is IBoutlet of UILabel.

step-5 : Put following code in ViewDidLoad.

override func viewDidLoad() {
        super.viewDidLoad()
        
        stepper_1.wraps = true
        stepper_1.autorepeat = true
        stepper_1.maximumValue = 40
        // Do any additional setup after loading the view, typically from a nib.
    }

Thank you...


No comments:

Post a Comment