Workshop 11: Display Today’s Date in a Text View

In the previous 10 workshops you learned the basics of creating an iOS application, connecting the view’s controls, and adding Swift code in the ViewController.swift class file to make the view a functional object.

Now, in this workshop, you will add Swift code in the Swift Variable application’s¬†actionShowOutput() function to do the following:

  1. Format today’s date and put it in a constant
  2. Display today’s date in the Text View

As you know, the Show Output button is connected to the actionShowOutput() function. Code you will add in the function will only work when you click the Show Output button.

Code

Here is the Swift code to add actionShowOutput() function.

@IBAction func actionShowOutput(sender: AnyObject) {
  //2. Format today's date and put it in a constant
  let formatter = NSDateFormatter()
  formatter.dateFormat = "EEE MMM dd, yyyy"
  let formatedDateString = formatter.stringFromDate(NSDate())

  //3. Display today's date in the Text View
  outputTextView.text = "Today's date is \(formatedDateString)"
}

output

Here is the output you will see on the iOS Simulator’s screen.
workshop11-fig1