Creating ReviewFormViewController

To process user input, you'll create ReviewFormViewController, which will grab all the values from the Review Form screen's fields and print them in the Debug area. You will learn how to store reviews in Chapter 22, Saving and Loading from Core Data. Do the following steps:

  1. Right-click the ReviewForm folder and select New File.
  2. iOS should already be selected. Choose Cocoa Touch Class and then click Next.
  3. Configure the file as follows:
  • Class: ReviewFormViewController
  • Subclass: UITableViewController
  • Also create XIB: Unchecked
  • Language: Swift
  • Click Next
  1. Click Create. ReviewFormViewController.swift will appear in the Project navigator.
  1. Delete everything after the viewDidLoad() method and add the following outlets after the class declaration. They correspond to the fields inside the Review Form screen:
@IBOutlet weak var ratingView: RatingsView!
@IBOutlet weak var tfTitle: UITextField!
@IBOutlet weak var tfName: UITextField!
@IBOutlet weak var tvReview: UITextView!
  1. You also need to configure the action for the Save button. Add the following code after the outlet declarations:
@IBAction func onSaveTapped(_ sender: Any) {
print(ratingView.rating)
print(tfTitle.text as Any)
print(tfName.text as Any)
print(tvReview.text as Any)
dismiss(animated: true, completion: nil)
}

As you can see, this method just prints the contents of the outlets to the Debug area and dismisses the Review Form screen.

Now, let's connect the outlets in ReviewFormViewController to the user interface elements in the Table View Controller Scene in ReviewForm.storyboard, as follows:

  1. Click ReviewForm.storyboard in the Project navigator and find the Table View Controller icon inside the Table View Controller Scene. Click the Identity inspector. Then, under Custom Class, set Class to ReviewFormViewController, as shown in the following screenshot. Note that Table View Controller Scene will change to Review Form View Controller Scene:

  1. Click the view in the document outline as shown, click the Identity inspector, and under Custom Class, set Class to RatingsView. The view's name will change to Ratings View, as shown in the following screenshot:


  1. Next, you will connect the outlets. Click the Review Form View Controller icon in the document outline and click the Connections inspector, as shown in the following screenshot:


  1. Connect the ratingView outlet to the Ratings View, as shown in the following screenshot:

  1. Connect the tfTitle outlet to the first Text Field, as shown in the following screenshot:

  1. Connect the tfName outlet to the second Text Field, as shown in the following screenshot:

  1. Connect the tvReview outlet to the Text View, as shown in the following screenshot:


  1. Finally, connect the onSaveTapped: action to the Save button, as shown in the following screenshot:

  1. Build and run your app. Go to the Review Form screen, add some sample text to the fields, and tap the Save button, as shown in the following screenshot:

  1. You'll see the data you entered appear in the Debug area, as follows:

Congratulations! The Review Form screen is now able to accept user input. You'll learn how to save and present the review data in a Chapter 22, Saving and Loading from Core Data.

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

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