Create a typical application in Swift

You’ll build a simple application with a submit button, an editable text field, and an output field.

      1. Create a simple single view application with a graphical user interface (GUI) that includes a text field, a label, and a button. When a user presses the button, the URL in the text field is sent to Watson, which analyzes it and returns an opinion that is shown in the output field.

    Take the text in the text field and echo it in the label field when the button is pressed.


    1. See the

Xcode documentation

    1.  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 URL to the document
    2. Select New Referencing Outlet from the list.
    3. Enter textField in the dialog.
    4. Insert the new reference between class ViewController and override func viewDidLoad.
    5. Click Connect.

      The inserted text is @IBOutlet weak var textField: UITextField!
    6. Connect the button. Insert the sent event named Touch up Inside before the last curly bracket.

      The inserted text is: @IBAction func checkButtonPressed(sender: AnyObject) {
    7. Connect the text label. Insert the parameter UILabel between @IBOutlet weak var textField: UITextField! and override func…

      The inserted text is @IBOutlet weak var textStatusLabel: UILabel!
    8. Test the code by adding NSLog(textField.text!) to the end of the checkButtonPressed method.
      NSLog allows you to log your actions. Setting textStatusLabel.text with textField.text allows you to show the entered text of the Text field in the Label field.
      import UIKit
      class ViewController: UIViewController {
      @IBOutlet weak var textField: UITextField!
      @IBOutlet weak var textStatusLabel: UILabel!

      override func viewDidLoad() {
      // Do any additional setup after loading the view, typically from a nib.

      override func didReceiveMemoryWarning() {
      // Dispose of any resources that can be recreated.

      @IBAction func checkButtonPressed(sender: AnyObject) {

      //checking sentiment

      //setting feedback on sentiment
      textStatusLabel.text = textField.text

    9. Build and execute your application.

If you see the text “” the GUI is working.