Chapter    17

Using Text with Labels, Text Fields, and Combo Boxes

When a program can offer a limited range of valid options to the user, that’s when you want to use check boxes, radio buttons, date pickers, or sliders. However, sometimes a program needs to allow the user to type in data that can never be predicted ahead of time, such as a person’s name. When a program needs to allow the user to type in data, that’s when you need to use a text field or a combo box.

A text field lets the user type in anything he or she wants including numbers, unusual characters, or ordinary letters. That means a program needs to verify that the user typed in valid data.

Sometimes a program can offer the user both the option of choosing from a limited selection of valid options or typing in data instead. For example, a program might ask the user to choose a country. Then the program can offer a limited range of likely options or give the user the freedom to type something else.

This ability to either choose from a limited range of options or type something in is the main advantage of a combo box. The combo box gets its name because it combines the features of a pop-up button (displaying a list of valid options) with the freedom to type anything in (like a text field).

While text fields and combo boxes are used to accept data from the user, labels are used to display information to the user. When you just need to display information such as displaying instructions or identifying the purpose of a text field (such as telling a user to type a name or address), you’ll want to use a label. Together, labels, text fields, and combo boxes work to accept and display text on a user interface.

Using Text Fields

Since a user can type anything into a text field, a text field stores data as a string, which you can retrieve using the stringValue property. However, since users can type in integer or decimal numbers, a text field is versatile enough to recognize such numbers.

If the user types in an integer, you can retrieve that value by accessing the intValue property. If the user types in a decimal number, you can retrieve that value by accessing either the floatValue or doubleValue properties.

No matter what the user types into a text field, you can retrieve the proper value. Since text fields can accept strings or numbers, your program can retrieve the proper value by accessing one of the following properties:

  • intValue – Retrieves an integer value. If the user types a string, this property stores 0. If the user types a decimal number such as 10.9, it stores only the integer value such as 10.
  • floatValue or doubleValue – Retrieves a floating-point or double value. If the user types a string, this property stores 0.0. If the user types an integer such as 4, the floatValue and doubleValue properties store it as a decimal such as 4.0.
  • stringValue – Retrieves a string value. If the user types a number, it stores that number as a string (such as “4.305”).

 Note  When the user types text into a text field, that typed text gets stored in all four properties: intValue, floatValue, doubleValue, and stringValue.

Beyond the standard text field, Xcode offers several text field variations for handling different types of text entry:

  • Text Field with Number Formatter – Defines the type of numbers that can be typed in
  • Secure Text Field – Masks typed text
  • Search Field – Stores a list of previously entered text
  • Token Field – Allows the user to enter tokens of content in addition to typing ordinary text

Using a Number Formatter

A number formatter lets you define valid numeric values the user can type into the text box such as a minimum or maximum value, or a specific way to enter data such as with a % sign or by typing the number out as “four” instead of typing 4.

Xcode gives you two ways to create a text field with a number formatter. First, you can drag and drop the Text Field with Number Formatter from the Object Library and place it on your user interface. Second, you can drag and drop the Number Formatter from the Object Library and place it on an existing text field on your user interface.

To quickest way to find both of these items is to type “Number Formatter” in the search text field at the bottom of the Object Library as shown in Figure 17-1.

9781484212349_Fig17-01.jpg

Figure 17-1. The Text Field with Number Formatter and Number Formatter in the Object Library

Whether you create a new text field with the Text Field with Number Formatter item or apply Number Formatter to an existing text field, you’ll need to define the number formatter settings. To do this, you need to follow several steps:

  1. Click the Show Document Outline icon to display the Document Outline.
  2. Click on the disclosure triangle to the left of the Text Field that uses the Number Formatter.
  3. Click on the disclosure triangle that appears to the left of the Text Field Cell underneath the text field.
  4. Click on the Number Formatter as shown in Figure 17-2.

    9781484212349_Fig17-02.jpg

    Figure 17-2. The Number Formatter appears in the Document Outline

  5. Choose View image Utilitiesimage Show Attributes Inspector. The Show Attributes Inspector pane appears in the upper right corner of the Xcode window as shown in Figure 17-3.

    9781484212349_Fig17-03.jpg

    Figure 17-3. The Show Attributes Inspector pane

The Minimum and Maximum check boxes let you define a minimum and/or maximum value that the text field will accept. If you define a minimum value of 10 and the user types in a number less than 10, the text field won’t accept the number.

The Localize check box tells Xcode to use the user’s local settings to determine the appearance of a decimal point and currency symbol. In some parts of the world, they use a period for a decimal point while in others they use a comma.

Likewise for currency, Europeans would use the Euro symbol, Americans would use the dollar symbol, and British users would use the British pound symbol. Selecting the Localize check box ensures that your text field number formatter works no matter what part of the world the program may be used.

The Style pop-up menu lets you define different ways to format numbers. When you choose a Style, the Unformatted and Formatted text fields under the Sample category shows you how your numbers may look.

For example, in Figure 17-3, choosing the None Style means that if the user types in 1,234.75 into a text field with a number formatter and presses Return, the text field formats that number as simply 1235.

However, if the Style is Currency, Percent, Scientific, or Spell Out, that means the user can enter a number as defined by the Formatted example, and the text field will store the number as shown in the Unformatted example.

To see how to use these different number formatters, follow these steps:

  1. From within Xcode choose File image New image Project.
  2. Click Application under the OS X category.
  3. Click Cocoa Application and click the Next button. Xcode now asks for a product name.
  4. Click in the Product Name text field and type NumberProgram.
  5. Make sure the Language pop-up menu displays Swift and that no check boxes are selected.
  6. Click the Next button. Xcode asks where you want to store the project.
  7. Choose a folder to store your project and click the Create button.
  8. Click the MainMenu.xib file in the Project Navigator. Your program’s user interface appears.
  9. Click the NumberProgram icon to display the window of your program’s user interface.
  10. Choose View image Utilities image Show Object Library. The Object Library appears in the bottom right corner of the Xcode window.
  11. Drag a Pop-Up Button, a Push Button, and a Text Field with Number Formatter on to the user interface window so it looks like Figure 17-4.

    9781484212349_Fig17-04.jpg

    Figure 17-4. The user interface of the NumberProgram

  12. Double-click on the pop-up button. Xcode displays a list of three choices labeled Item 1, Item 2, and Item 3 as shown in Figure 17-5.

    9781484212349_Fig17-05.jpg

    Figure 17-5. Modifying a pop-up button

  13. Double-click on Item 1 and when Xcode highlights it, type None.
  14. Double-click on Item 2 and when Xcode highlights it, type Decimal.
  15. Double-click on Item 3 and when Xcode highlights it, type Currency.
  16. Drag three Menu Items from the Object Library to the bottom of the pop-up button list.
  17. Double-click on each of these three new menu items and modify the name so it displays Percent, Scientific, and Spell Out.

    9781484212349_Fig17-06.jpg

    Figure 17-6. The modified pop-up button menu

  18. Choose View image Assistant Editor image Show Assistant Editor. The AppDelegate.swift file appears next to your user interface.
  19. Move the mouse pointer over the pop-up button, hold down the Control key, and drag underneath the IBOutlet line.
  20. Release the Control key and the mouse. A pop-up window appears.
  21. Click in the Name text field, type popUpChoice, and click the Connect button. An IBOutlet appears.
  22. Move the mouse pointer over the text field, hold down the Control key, and drag the mouse under the IBOutlet line.
  23. Release the Control key and the mouse. A pop-up window appears.
  24. Click in the Name text field, type textBox, and click the Connect button.
  25. Move the mouse pointer over the label, hold down the Control key, and drag the mouse under the IBOutlet line.
  26. Release the Control key and the mouse. A pop-up window appears.
  27. Click in the Name text field, type labelResult, and click the Connect button.
  28. Click the Show Document Outline icon to reveal the Document Outline.
  29. Move the mouse pointer over Number Formatter, hold down the Control key, and drag the mouse under the IBOutlet line.
  30. Release the Control key and the mouse. A pop-up window appears.
  31. Click in the Name text field, type numberFormat, and click the Connect button. You should have four IBOutlet lines as follows:
    @IBOutlet weak var popUpChoice: NSPopUpButton!
    @IBOutlet weak var textBox: NSTextField!
    @IBOutlet weak var labelResult: NSTextField!
    @IBOutlet weak var numberFormat: NSNumberFormatter!
  32. Click the Hide Document Outline icon to hide the document outline.
  33. Move the mouse pointer over the push button, hold down the Control key, and drag the mouse above the last curly bracket in the bottom of the AppDelegate.swift file.
  34. Release the Control key and the mouse button. A pop-up window appears.
  35. Click in the Connect pop-up menu and choose Action.
  36. Click in the Name text field and type showResults.
  37. Click in the Type pop-up menu, choose NSButton, and click the Connect button. Xcode creates an empty IBAction method.
  38. Modify the IBAction method as follows:
    @IBAction func showResults(sender: NSButton) {
        if popUpChoice.titleOfSelectedItem! == "None" {
            numberFormat.numberStyle = NSNumberFormatterStyle.NoStyle
        } else if popUpChoice.titleOfSelectedItem! == "Decimal" {
            numberFormat.numberStyle = NSNumberFormatterStyle.DecimalStyle
        } else if popUpChoice.titleOfSelectedItem! == "Currency" {
            numberFormat.numberStyle = NSNumberFormatterStyle.CurrencyStyle
        } else if popUpChoice.titleOfSelectedItem! == "Percent" {
            numberFormat.numberStyle = NSNumberFormatterStyle.PercentStyle
        } else if popUpChoice.titleOfSelectedItem! == "Scientific" {
            numberFormat.numberStyle = NSNumberFormatterStyle.ScientificStyle
        } else if popUpChoice.titleOfSelectedItem! == "Spell Out" {
            numberFormat.numberStyle = NSNumberFormatterStyle.SpellOutStyle
        }
        labelResult.stringValue = String(stringInterpolationSegment: textBox.doubleValue)
    }
  39. Choose Product image Run. Your user interface appears.
  40. Click on the pop-up button, choose Decimal, type a decimal number into the text field, and then click on the Push Button. The label displays the result that the text field’s number formatter contains.
  41. Click on the pop-up button, choose Currency, type $4.56 into the text field, and then click on the Push Button. The label displays 4.56.
  42. Click on the pop-up button, choose Percent, type 25% into the text field, and then click on the Push Button. The label displays 0.25.
  43. Click on the pop-up button, choose Scientific, type 1.2E3 into the text field, and then click on the Push Button. The label displays 1200.0.
  44. Click on the pop-up button, choose Spell Out, type forty-five into the text field, and then click on the Push Button. The label displays 45.0.
  45. Choose NumberProgram image Quit NumberProgram.

The above Swift code in the IBAction method uses the titleOfSelectedItem property to determine which option the user chose. Then it uses multiple if-else if statements to change the number Style property of the number formatter.

The pop-up button lets you choose different number formatting styles so you can see how they work when you type a formatted number into the text field. By choosing different formatting styles, you can see how the text field lets you type in differently formatted numbers such as 25% (which gets converted into 0.25) or 1.2E3 (which gets converted into 1200.0).

Using a Secure Text Field, a Search Field, and a Token Field

A secure text field simply hides text behind bullets when the user types. This can be handy for masking actual text such as passwords, credit card numbers, or other sensitive information from view. Beyond masking anything typed in, a secure text field works exactly like a regular text field where you can retrieve the value typed using the intValue, floatValue, doubleValue, or stringValue properties.

A search text field looks like a typical search field you might find in a browser that displays a magnifying glass icon and close icon that can delete the text in the search field with one click. Each time the user types something in the search field and presses Return, the search field stores the text as a list stored in the recentSearches property.

By accessing this recentSearches property, you can view a list of everything the user previously typed in that search field.

A token field lets you type text and then it encloses that text inside a token. Despite the appearance of tokens, you can still retrieve the text of a token field by accessing the stringValue property.

To see how to use a secure text field, a search field and a token field, follow these steps:

  1. From within Xcode choose File image New image Project.
  2. Click Application under the OS X category.
  3. Click Cocoa Application and click the Next button. Xcode now asks for a product name.
  4. Click in the Product Name text field and type TextProgram.
  5. Make sure the Language pop-up menu displays Swift and that all check boxes are clear and not selected.
  6. Click the Next button. Xcode asks where you want to store the project.
  7. Choose a folder to store your project and click the Create button.
  8. Click on the MainMenu.xib file in the Project Navigator Pane.
  9. Click on the TextProgram icon to make the window of the user interface appear.
  10. Drag a three Push Buttons, a Search Field, a Token Field, a Secure Text Field, and three labels resized to expand their width on to the user interface as shown in Figure 17-7.

    9781484212349_Fig17-07.jpg

    Figure 17-7. The user interface of the TextProgram

  11. Choose View image Assistant Editor image Show Assistant Editor. Xcode displays the AppDelegate.swift file next to the user interface.
  12. Move the mouse pointer over the Search Field, hold down the Control key, and drag the mouse under the IBOutlet line in the AppDelegate.swift file.
  13. Release the Control key and the mouse button. A pop-up window appears.
  14. Click in the Name text field, type searchBox, and click the Connect button.
  15. Move the mouse pointer over the label to the right of the Search Field, hold down the Control key, and drag the mouse under the IBOutlet line.
  16. Release the Control key and the mouse button. A pop-up window appears.
  17. Click in the Name text field, type historyBox, and click the Connect button. You should have two IBOutlets like this:
    @IBOutlet weak var searchBox: NSSearchField!
    @IBOutlet weak var historyBox: NSTextField!
  18. Move the mouse pointer over the Token Field, hold down the Control key, and drag the mouse under the IBOutlet line in the AppDelegate.swift file.
  19. Release the Control key and the mouse button. A pop-up window appears.
  20. Click in the Name text field, type tokenBox, and click the Connect button.
  21. Move the mouse pointer over the label to the right of the Search Field, hold down the Control key, and drag the mouse under the IBOutlet line.
  22. Release the Control key and the mouse button. A pop-up window appears.
  23. Click in the Name text field, type tokenResult, and click the Connect button. You should have two more IBOutlets like this:
        @IBOutlet weak var tokenBox: NSTokenField!
        @IBOutlet weak var tokenResult: NSTextField!
  24. Move the mouse pointer over the Secure Text Field, hold down the Control key, and drag the mouse under the IBOutlet line in the AppDelegate.swift file.
  25. Release the Control key and the mouse button. A pop-up window appears.
  26. Click in the Name text field, type secureBox, and click the Connect button.
  27. Move the mouse pointer over the label to the right of the Search Field, hold down the Control key, and drag the mouse under the IBOutlet line.
  28. Release the Control key and the mouse button. A pop-up window appears.
  29. Click in the Name text field, type secureResults, and click the Connect button. You should have two more IBOutlets like this:
        @IBOutlet weak var secureBox: NSSecureTextField!
        @IBOutlet weak var secureResults: NSTextField!
  30. Move the mouse pointer over the Push Button under the Search Field, hold down the Control key, and drag the mouse over the last curly bracket in the bottom of the AppDelegate.swift file.
  31. Release the Control key and the mouse. A pop-up window appears.
  32. Click in the Connect pop-up menu and choose Action.
  33. Click in the Name text field and type showHistory.
  34. Click in the Type pop-up menu and choose NSButton. Then click the Connect button. Xcode creates an IBAction method.
  35. Move the mouse pointer over the Push Button under the Token Field, hold down the Control key, and drag the mouse over the last curly bracket in the bottom of the AppDelegate.swift file.
  36. Release the Control key and the mouse. A pop-up window appears.
  37. Click in the Connect pop-up menu and choose Action.
  38. Click in the Name text field and type showToken.
  39. Click in the Type pop-up menu and choose NSButton. Then click the Connect button. Xcode creates an IBAction method.
  40. Move the mouse pointer over the Push Button under the Secure Text Field, hold down the Control key, and drag the mouse over the last curly bracket in the bottom of the AppDelegate.swift file.
  41. Release the Control key and the mouse. A pop-up window appears.
  42. Click in the Connect pop-up menu and choose Action.
  43. Click in the Name text field and type showSecret.
  44. Click in the Type pop-up menu and choose NSButton. Then click the Connect button. Xcode creates an IBAction method.
  45. Modify these IBAction methods as follows:
    @IBAction func showHistory(sender: NSButton) {
        historyBox.stringValue = String(stringInterpolationSegment: searchBox.recentSearches)
    }

    @IBAction func showToken(sender: NSButton) {
        tokenResult.stringValue = tokenBox.stringValue
    }

    @IBAction func showSecret(sender: NSButton) {
        secureResults.stringValue = secureBox.stringValue
    }
  46. Choose Product image Run. Your user interface appears.
  47. Click in the Search Field, type Yahoo, and press Return.
  48. Click the close icon (the X in the gray circle) to clear the Search Field, type Google, and press Return.
  49. Click the push button underneath the Search Field. Notice that the label to the right displays the two search results.
  50. Click in the Token field, type Yahoo, and press Return. Notice that the token field displays your typed text as a token.
  51. Click in the Token field and next to the Yahoo token, type Google and press Return.
  52. Click the push button underneath the Token field. Notice that the label to the right displays your two token texts.
  53. Click in the Secure Text Field and type password. Notice that the Secure Text Field masks the text.
  54. Click the push button underneath the Secure Text Field. Notice the label to the right displays the text from the Secure Text Field as shown in Figure 17-8.

    9781484212349_Fig17-08.jpg

    Figure 17-8. The results of the TextProgram

  55. Choose TextProgram image Quit TextProgram.

Although Search Fields, Token Fields, and Secure Text Fields look differently, they behave exactly like a text field when you need to access the values stored inside them.

Using Combo Boxes

A text field lets the user type in anything while a pop-up button displays a fixed range of options. When you want to display a fixed range of options and let the user type in text, that’s when you can use a combo box.

A combo box is based on the NSComboBox class, which is based on the NSTextField class that defines all text fields. As a result, you can retrieve values from a combo box using the intValue, floatValue, doubleValue, or stringValue properties.

When using a combo box, you have two ways to fill its pop-up list of options. First, you can create an internal list using the Show Attributes Inspector pane. Second, you can use an external data source so the combo box retrieves data from a database file or over the Internet.

Creating an Internal List

An internal list is best when you know ahead of time exactly how many and which items you want to store in a combo box, such as a list of countries or product names. To create an internal list, follow these steps:

  1. Place a combo box on your program’s user interface.
  2. Click to select that combo box.
  3. Choose View image Utilities image Show Attributes Inspector. The Items list appears as shown in Figure 17-9.

9781484212349_Fig17-09.jpg

Figure 17-9. The Items list lets you define items to appear in a combo box

To edit an existing item in the list, click on that item to select it and press Return. Now you can edit the text.

To remove an existing item, click on that item to select it and click the minus sign icon.

To add a new item, click on the plus sign icon. Then click on the newly added item, press Return, and edit the text.

The Visible Items text field defines how many items the combo box menu displays.

The Autocompletes check box lets the combo box displays items in its list that matches what the user started to type in. So if the user types one letter, autocomplete displays a list of all items in the combo box list that begins with the same letter the user typed.

The Uses Data Source check box determines if the combo box uses its internal Items list to display menu items, or retrieves data from another source. If the Uses Data Source check box is selected, then you’ll need to write Swift code to fill a combo box with data.

To see how to fill a combo box using an internal list, follow these steps:

  1. From within Xcode choose File image New image Project.
  2. Click Application under the OS X category.
  3. Click Cocoa Application and click the Next button. Xcode now asks for a product name.
  4. Click in the Product Name text field and type ComboProgram.
  5. Make sure the Language pop-up menu displays Swift and that all check boxes are clear and not selected.
  6. Click the Next button. Xcode asks where you want to store the project.
  7. Choose a folder to store your project and click the Create button.
  8. Click on the MainMenu.xib file in the Project Navigator Pane.
  9. Click on the ComboProgram icon to make the window of the user interface appear.
  10. Drag a Combo Box, Push Button, and a Label on to the user interface, and widen the label so it looks like Figure 17-10.

    9781484212349_Fig17-10.jpg

    Figure 17-10. The user interface of the ComboProgram

  11. Choose View image Assistant Editor image Show Assistant Editor. Xcode shows the AppDelegate.swift file next to the user interface.
  12. Move the mouse pointer over the Combo Box, hold down the Control key, and drag the mouse under the IBOutlet line in the AppDelegate.swift file.
  13. Release the Control key and the mouse. A pop-up window appears.
  14. Click in the Name text field, type myCombo, and click the Connect button.
  15. Move the mouse pointer over the Label, hold down the Control key, and drag the mouse under the IBOutlet line in the AppDelegate.swift file.
  16. Release the Control key and the mouse. A pop-up window appears.
  17. Click in the Name text field, type comboResult, and click the Connect button. You should now have these two IBOutlets:
    @IBOutlet weak var myCombo: NSComboBox!
    @IBOutlet weak var comboResult: NSTextField!
  18. Move the mouse pointer over the Push Button, hold down the Control key, and drag the mouse just above the last curly bracket at the bottom of the AppDelegate.swift file.
  19. Release the Control key and the mouse. A pop-up window appears.
  20. Click in the Connection pop-up menu and choose Action.
  21. Click in the Name text field and type showResult.
  22. Click in the Type pop-up menu and choose NSButton. Then click the Connect button. Xcode creates an empty IBAction method.
  23. Modify this IBAction method as follows:
    @IBAction func showResult(sender: NSButton) {
        comboResult.stringValue = myCombo.stringValue
    }
  24. Choose Product image Run. Your user interface appears.
  25. Click in the combo box and type Hello.
  26. Click the push button. The word Hello appears in the label.
  27. Click in downward-pointing arrow on the right edge of the combo box to display its list of items. Since we didn’t modify the default list, the combo box list contains three items labeled Item 1, Item 2, and Item 3.
  28. Click on Item 3 and then click the push button. The label underneath displays Item 3.
  29. Choose ComboProgram image Quit ComboProgram.

This example used the default item list for the combo box. Experiment by adding or modifying this combo box item list and select the Autocompletes check box to see how these options modify the combo box.

Using a Data Source

A data source is best when a combo box needs to display data that may change over time, such as displaying a list of information from a database. To use a data source, you need to write Swift code. First, you need to adopt the NSComboBoxDataSource protocol in a class like this:

class AppDelegate: NSObject, NSApplicationDelegate, NSComboBoxDataSource {

Next, you need to write two functions. One function needs to count the number of items to display in the combo box. The second function needs to retrieve data to insert in the combo box’s list of items.

Finally, you need to specify a data source (the file that contains the data you want to display in the combo box) and make sure the usesDataSource property for the combo box is selected in the Show Attributes Inspector pane or set to true using Swift code.

To see how a data source works to fill a combo box with a list of items, follow these steps:

  1. Make sure the ComboProgram project is loaded in Xcode.
  2. Modify the class name to include the NSComboBoxDataSource protocol as follows:
    class AppDelegate: NSObject, NSApplicationDelegate, NSComboBoxDataSource {
  3. Click on the AppDelegate.swift file in the Project Navigator pane.
  4. Underneath the IBOutlet lines, add the following line to create an array of strings that will fill the combo box’s item list:
    let myArray = ["Sandwich", "Chips", "Soda", "Salad"]
  5. Modify the applicationDidFinishLaunching function as follows:
    func applicationDidFinishLaunching(aNotification: NSNotification) {
        // Insert code here to initialize your application
        myCombo.dataSource = self
        myCombo.usesDataSource = true

        numberOfItemsInComboBox(myCombo)
        comboBox(myCombo, objectValueForItemAtIndex: 0)
    }

    This Swift code defines the data source for the combo box (identified by the myCombo IBOutlet) as self, which means the same class is also the data source. It makes sure the usesDataSource property is true to use a data source. Then it calls two functions defined by the NSComboBoxDataSource protocol.

  6. Above the IBAction method, add the following two function implementations:
    func numberOfItemsInComboBox(aComboBox: NSComboBox) -> Int {
        return myArray.count
    }

    func comboBox(aComboBox: NSComboBox, objectValueForItemAtIndex index: Int) -> AnyObject {
        return myArray[index]
    }

    The numberOfItemsInComboBox function counts all the items in the myArray array and returns that integer. The comboBox function places each array item in the combo box’s item list.

  7. Choose Product image Run.
  8. Click on the downward-pointing arrow of the combo box. Notice that its item list contains the items defined in myArray (“Sandwich,” “Chips,” “Soda,” and “Salad”).
  9. Click on Sandwich in the combo box to select it and then click the push button. Notice that the label underneath displays Sandwich.
  10. Choose ComboProgram image Quit ComboProgram.

This simple program shows how to retrieve a list of items from another data source (in this case an array called myArray) to fill an item list for a combo box. The complete code for the AppDelegate.swift file should look like this:

import Cocoa

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate, NSComboBoxDataSource {

    @IBOutlet weak var window: NSWindow!
    @IBOutlet weak var myCombo: NSComboBox!
    @IBOutlet weak var comboResult: NSTextField!

    let myArray = ["Sandwich", "Chips", "Soda", "Salad"]

    func applicationDidFinishLaunching(aNotification: NSNotification) {
        // Insert code here to initialize your application
        myCombo.usesDataSource = true
                     myCombo.dataSource = self

        numberOfItemsInComboBox(myCombo)
        comboBox(myCombo, objectValueForItemAtIndex: 0)
    }

    func applicationWillTerminate(aNotification: NSNotification) {
        // Insert code here to tear down your application
    }

    func numberOfItemsInComboBox(aComboBox: NSComboBox) -> Int {
        return myArray.count
    }

    func comboBox(aComboBox: NSComboBox, objectValueForItemAtIndex index: Int) -> AnyObject {
        return myArray[index]
    }

    @IBAction func showResult(sender: NSButton) {
        comboResult.stringValue = myCombo.stringValue
    }

}

Summary

Text fields allow the user to type anything such as numbers or strings, so a text field offers multiple properties (intValue, floatValue, doubleValue, and stringValue) to store typed information in the proper data type. To restrict numeric information the user may type, you can use a number formatter to define minimum and/or maximum values along with defining different ways the user can enter numeric data such as with a percentage sign or a currency sign.

Besides ordinary text fields, Xcode offers several variations of text fields for specialized purposes. Labels are designed for simply displaying text without allowing the user to edit the displayed text. A secure text field masks typed in data to keep it safe from prying eyes. A token field displays typed in data as groups or tokens while a search field offers a property to remember previously typed in data.

Combo boxes combine the features of a text field with a pop-up button. A combo box can display a list of valid options that the user can click on, or the user can type in data. You can define a combo box’s list of options in the Show Attributes Inspector pane, or you can retrieve data from another source such as from an array.

The main purpose of text fields and combo boxes is to allow the user to type in any type of data. When the user types in data, you may need to write Swift code to verify that the entered data is actually valid for your program.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset