[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.
Displaying the three-dimensional structure of DNA and proteins in Ruby-in the case of GR.rb
I received the data of the journey (diary application) in Java and visualized it # 001
[Order method] Set the order of data in Rails
Check the behavior of getOne, findById, and query methods in Spring Boot + Spring Data JPA
[For beginners] DI ~ The basics of DI and DI in Spring ~
Until the use of Spring Data and JPA Part 1
[Swift] Use UserDefaults to save data in the app
Find the approximate value of log (1 + x) in Swift
[Spring Data JPA] Can And condition be used in the automatically implemented method of delete?