[SWIFT] How to use Segmented Control and points to note

Premise

This is a memorandum of the parts that are prone to mistakes when using Segmented Control.

How to use

[1] Place parts

スクリーンショット 2020-10-28 22.47.26.png スクリーンショット 2020-10-28 22.50.04.png

[2] Edit parts

名称未設定2.001.jpeg 名称未設定2.002.jpeg
Completed form
スクリーンショット 2020-10-28 22.51.44.png

[3] Coding

Declaration of each part

ViewController.swift


@IBOutlet weak var selectSegmentedControl: UISegmentedControl!

@IBOutlet weak var DisplayLabel: UILabel!

Add action

ViewController.swift


@IBAction func tappedSegmentedControl(_ sender: UISegmentedControl) {
  
}

Write the process with a switch statement in the action.

ViewConroller.swift


   switch sender.selectedSegmentIndex {
        case 0:
            print("Selected: iOS")
            self.DisplayLabel.text = "iOS"
        case 1:
            print("Selected: iPadOS")
            self.DisplayLabel.text = "iPadOS"
        case 2:
            print("Selected: watchOS")
            self.DisplayLabel.text = "watchOS"
        case 3:
            print("Selected: tvOS")
            self.DisplayLabel.text = "tvOS"
        case 4:
            print("Selected: macOS")
            self.DisplayLabel.text = "macOS"
        default:
            print("Selected: Not selected")
            self.DisplayLabel.text = "Unselected"
        }


Whole code

ViewConroller.swift


@IBOutlet weak var selectSegmentedControl: UISegmentedControl!
    
@IBOutlet weak var DisplayLabel: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.DisplayLabel.text = "Unselected"
    }
    
@IBAction func tappedSegmentedControl(_ sender: UISegmentedControl) {
        switch sender.selectedSegmentIndex {
        case 0:
            print("Selected: iOS")
            self.DisplayLabel.text = "iOS"
        case 1:
            print("Selected: iPadOS")
            self.DisplayLabel.text = "iPadOS"
        case 2:
            print("Selected: watchOS")
            self.DisplayLabel.text = "watchOS"
        case 3:
            print("Selected: tvOS")
            self.DisplayLabel.text = "tvOS"
        case 4:
            print("Selected: macOS")
            self.DisplayLabel.text = "macOS"
        default:
            print("Selected: Not selected")
            self.DisplayLabel.text = "Unselected"
     }
}

[4] Association

Associate Outlet and Action. 名称未設定2.001.jpeg 名称未設定2.002.jpeg 名称未設定2.003.jpeg

⚠️ Attention point ⚠️

If you say "Because it's always Touch Up Inside" and associate it with Touch Up Inside **, it won't work! ** ** Let's firmly connect ** Value Changed and **. 名称未設定2.004.jpeg

Completed GIF

ezgif.com-gif-maker.gif

Sample project

https://github.com/rea-sna/SegmentedControl_Sample

Thank you for watching until the end ☺︎

Recommended Posts

How to use Segmented Control and points to note
How to use StringBurrer and Arrays.toString.
[Rails] How to use devise (Note)
How to use EventBus3 and ThreadMode
How to use equality and equality (how to use equals)
How to use OrientJS and OrientDB together
How to set up and use kapt
How to use Java Scanner class (Note)
[Note] How to use Rails 6 Devise + cancancan
How to use substring and substr methods
Ruby: CSV :: How to use Table Note
Note: [Docker] How to start and stop
How to use @Builder and @NoArgsConstructor together
[Java] How to use FileReader class and BufferedReader class
How to use Map
How to use rbenv
How to use letter_opener_web
How to use with_option
How to use fields_for
How to use java.util.logging
[Ruby] How to use gsub method and sub method
How to use collection_select
Note how to use Swift super basic TableView
How to use Twitter4J
How to use active_hash! !!
How to use MapStruct
How to use hidden_field_tag
How to use TreeSet
[How to use label]
How to use identity
How to use hashes
How to use scope and pass processing (Jakarta)
[Java] Note how to use RecyclerView and implementation of animated swipe processing.
How to use JUnit 5
How to use Dozer.mapper
How to use Gradle
How to use org.immutables
How to use java.util.stream.Collector
How to use VisualVM
How to use Map
[Java] How to use Calendar class and Date class
How to use RealSense with ubuntu 20.04 and ROS Noetic
How to use and apply Java's JFrame / Canvas class
[Java] How to use Map
[Java] How to use Map
How to use Priority Queuing
[Rails] How to use enum
How to use java Optional
How to use JUnit (beginner)
How to use Ruby return
[Swift] Let's use Segmented Control
[Rails] How to use enum
[Swift] How to use UserDefaults
How to use java class
How to use Big Decimal
[Java] How to use Optional ②
[Java] How to use removeAll ()
How to use String [] args
[Java] How to use string.format
[Note] How to write Dockerfile/docker-compose.yml
How to use rails join