[Swift] The process of registering the item selected in "UISegmentedControl" in Realm and displaying the registration data.

I got stuck about the above while creating the iOS App, so I will record it as a memorandum.

Process to register the item selected in "UISegmentedControl" to Realm

スクリーンショット 2020-10-29 0.59.58.png After setting "UISegmentedController" on the storyboard Describe the following sources in the target class.

ViewController.swift


@IBOutlet weak var test: UISegmentedControl! 
        //Specify the table created in RealmDB
        let results = realm.objects(testTable.self)
        //Specify table name
        let Test = testTable()
        //"UISegmentedControl"Select the item in"selectedIndex"Store in
        //In this case 0 if First is selected,1 is when Second is selected"selectedIndex"Stored in
        let selectedIndex = test.selectedSegmentIndex
        //Register with Realm object
        try! realm.write {
          Test.aaa = test.titleForSegment(at: selectedIndex)!
       }

Processing to display the data registered in Realm with "UISegmentedControl"

ViewController.swift


@IBOutlet weak var test: UISegmentedControl! 
 //Specify the acquisition data destination
  let results = realm.objects(testTable.self).filter("Id == 1").first
        if results?.aaa == "First" {
            test.selectedSegmentIndex = 0
        }else if results?.aaa == "Second" {
            test.selectedSegmentIndex = 1
        }

Recommended Posts

[Swift] The process of registering the item selected in "UISegmentedControl" in Realm and displaying the registration data.
How to change the maximum and maximum number of POST data in Spark
Displaying the three-dimensional structure of DNA and proteins in Ruby-in the case of GR.rb
[Order method] Set the order of data in Rails
Check the behavior of getOne, findById, and query methods in Spring Boot + Spring Data JPA
Until the use of Spring Data and JPA Part 2
Until the use of Spring Data and JPA Part 1
Find the approximate value of log (1 + x) in Swift