Create an application in Swift

You’ll build a simple application with a Submit button and an editable text field. There is no output field because the output is going to be delivered through voice.

Create a simple single view application with a graphical user interface (GUI) that includes a text field, a segmented control and a button. When a user presses the button, the text in the text field is sent to Watson, which analyzes it and returns a voice file (in the provided voice). This will be a single view application.

You take the text in the text field and simply log it in the console when the button is pressed.
See the Xcode documentation for information about how to create basic GUIs in Xcode.

Connect the GUI in Main.storyboard to the code in the ViewController file:

  1. Double-click TextField (by pressing the mouse pad with two fingers), which contains the text “Hello this is Watson speaking.”
  2. Select New Reference Outlet from the list.
  3. In the dialog, enter speakText.
  4. Insert the new reference between class ViewController and override func viewDidLoad.
  5. Click Connect.
    The result is @IBOutlet weak var speakText: UITextField!
  6. Connect the button by entering speakButtonPressed in the dialog.
    The inserted text is:
    @IBAction func speakButtonPressed(sender: AnyObject) {
    }
  7. Test the code by adding NSLog(speakText.text!) to the end of the checkButtonPressed method.
  8. Right-click the Segmented Control, which contains three controls on the right pane in the Attributes Inspector, and increase the number of segments to 3. Override the previous titles with Watson and Allyson respectively, and add the title “Kate” to the segment number 2.
  9. Double-click to select New Reference Outlet from the drop-down list.
  10. In the dialog, enter voiceSegment and insert it between Class View Controller and override func viewDidLoad.
  11. Click Connect.
    The result is: @IBOutlet weak var voiceSegment: UISegmentedControl!
    NSLog allows you to log your actions on the console. The code should look like this:
    Import UIKit
    class ViewController: UIViewController {
    @IBOutlet weak var textField: UITextField!
    @IBOutlet weak var voiceSegment: UISegmentedControl!

    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.
    }

    @IBAction func speakButtonPressed(sender: AnyObject) {
    NSLog(“Speak button pressed, speak:” + speakText.text! + “voice segment” + voiceSegment.selectedSegmentIndex.description)

    //add Watson Service

    }
    }

  12. Build and execute your application.