Home > Articles > Operating Systems, Server > MAC OS X/Other

  • Print
  • + Share This
Like this article? We recommend

Creating the iOS Application

Now let's write the code for the iOS application:

  1. First we'll populate the View window in the Main.Storyboard file with the following views (see Figure 6):

    • Label
    • Picker
    Figure 6

    Figure 6 Populating the View window in the containing iOS app. (The list of city names is just a placeholder.)

  2. Create an outlet for the Picker view by using the drag-and-drop method (via the Show Editor window). This action will create the statement shown in bold below in the ViewController.swift file:

    import UIKit
    
    class ViewController: UIViewController {
    
        @IBOutlet weak var pickerCountries: UIPickerView!
    
        override func viewDidLoad() {
            super.viewDidLoad()
            // Do any additional setup after loading the view, typically
            // from a nib.
        }
    
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }
    
    
    }
  3. Add the following statements in bold to the ViewController.swift file to populate the Picker view with a list of countries. When a country is selected, the value of the country will be saved using the NSUserDefaults class:

    import UIKit
    
    class ViewController: UIViewController,
                          UIPickerViewDataSource,
                          UIPickerViewDelegate {
    
        @IBOutlet weak var pickerCountries: UIPickerView!
    
        //---replace "group.net.learn2develop.articles.sharingdata"
        // with the string that you have created earlier---
        var defaults = NSUserDefaults(
            suiteName: "group.net.learn2develop.articles.sharingdata")
    
        var countries: [String]!
    
        override func viewDidLoad() {
            super.viewDidLoad()
            countries = ["Singapore", "Norway", "Japan", "Thailand",
                         "Hong Kong"]
            self.pickerCountries.delegate = self
            self.pickerCountries.dataSource = self
        }
        func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
            return 1
        }
    
        func pickerView(pickerView: UIPickerView,
            numberOfRowsInComponent component: Int) -> Int {
            return countries.count
        }
    
        func pickerView(pickerView: UIPickerView, titleForRow row: Int,
            forComponent component: Int) -> String! {
            return countries[row]
        }
    
        //---read a value from the setting, given a key---
        func readSettingValue(key:String) -> String? {
            return defaults?.objectForKey(key) as? String
        }
    
        //---save a setting value, given the key---
        func saveSettingValue(key:String, value:String) {
            defaults?.setObject(value, forKey: key)
            defaults?.synchronize()
        }
    
        //---when the user selects a country---
        func pickerView(pickerView: UIPickerView, didSelectRow row: Int,
            inComponent component: Int) {
            saveSettingValue("country", value: countries[row])
        }
    
        //---when the view appears---
        override func viewDidAppear(animated: Bool) {
            if let countrysSelected = self.readSettingValue("country") {
                //---set the picker to display the previously
                // selected country---
                self.pickerCountries.selectRow(
                    find(countries, countrysSelected)!,
                    inComponent: 0, animated: true)
            }
        }
    
        override func didReceiveMemoryWarning() {
           super.didReceiveMemoryWarning()
           // Dispose of any resources that can be recreated.
        }
    }
  4. Select the SharingData scheme at the top of Xcode and run the application on the iPhone Simulator. Select a country as shown in Figure 7.

    Figure 7

    Figure 7 Selecting a country from the Picker view.

  • + Share This
  • 🔖 Save To Your Account