The steps for using Guestbook List Screenlet are the same as those for using any Screenlet:
-
Insert the Screenlet in the storyboard scene where you want it to appear. You do this by adding an empty view to the scene, and then setting the Screenlet class as the view’s custom class.
-
Conform the scene’s view controller class to the Screenlet’s delegate protocol. This lets the view controller respond to the Screenlet’s events.
You’ll follow these steps to use Guestbook List Screenlet in the guestbooks scene. You’ll also take an extra step to trigger the segue to the entries scene when a user selects a guestbook.
Adding Guestbook List Screenlet to the Guestbooks Scene
Follow these steps to add Guestbook List Screenlet to the guestbooks scene:
-
In your storyboard, first select the guestbooks scene’s view controller. Then drag and drop a plain view (
UIView
) from the Object Library to the view controller. In the outline view, this new view should be nested under the view controller’s existing view.Figure 1: The new view is nested under the view controller's existing view.
-
Resize the new view to take up all the space below the navigation bar. With the new view selected, open the Add New Constraints menu at the bottom-right of the canvas. In this menu, set Spacing to nearest neighbor to 0 on all sides, and click the Add 4 Constraints button.
Figure 2: Set the new view's *Spacing to nearest neighbor* constraints to 0 on all sides.
-
With the new view still selected, open the Identity inspector and set the view’s custom class to
GuestbookListScreenlet
. The view now appears as Guestbook List Screenlet in the outline view.
Fantastic! The guestbooks scene now contains Guestbook List Screenlet. Next, you’ll conform the scene’s view controller class to the Screenlet’s delegate.
Conforming to the Screenlet’s Delegate Protocol
Recall that a view controller can respond to a Screenlet’s events by conforming
to the Screenlet’s delegate protocol. To respond to Guestbook List Screenlet’s
events, GuestbooksViewController
(the guestbooks scene’s view controller
class) must conform to the GuestbookListScreenletDelegate
protocol. You
created this delegate when creating the Screenlet. This delegate defines methods
for responding to the success or failure to retrieve guestbooks, and the
selection of a guestbook in the list.
Follow these steps to conform GuestbooksViewController
to the
GuestbookListScreenletDelegate
protocol:
-
Import
LiferayScreens
, and in the class declaration setGuestbooksViewController
to adopt theGuestbookListScreenletDelegate
protocol. The first few lines of the class should look like this:import UIKit import LiferayScreens class GuestbooksViewController: UIViewController, GuestbookListScreenletDelegate {...
-
Implement the
GuestbookListScreenletDelegate
methodscreenlet(_:onGuestbookListResponse:)
. Recall that this method lets you respond to a successful server call. Its arguments include theGuestbookModel
objects that result from such a call. Since the Screenlet already displays these objects, you don’t need to do anything in this method:func screenlet(screenlet: GuestbookListScreenlet, onGuestbookListResponse guestbooks: [GuestbookModel]) { }
-
Implement the
GuestbookListScreenletDelegate
methodscreenlet(_:onGuestbookListError:)
. Recall that this method lets you respond to a failed server call. Its arguments include theNSError
object that results from such a call. You don’t have to do anything in this method, but it’s a good idea to print the error:func screenlet(screenlet: GuestbookListScreenlet, onGuestbookListError error: NSError) { print("Failed to retrieve guestbooks: \(error.localizedDescription)") }
-
Implement the
GuestbookListScreenletDelegate
methodscreenlet(_:onGuestbookSelected:)
. Recall that this method lets you respond when a user selects a guestbook in the list. It does so by including the selectedGuestbookModel
object in its arguments. When a user selects a guestbook, the app should transition to the entries scene and display that guestbook’s entries with Entry List Screenlet. To do this, you must trigger the segue to the entries scene by using the methodperformSegue(withIdentifier:sender:)
with the segue’s ID and the selectedGuestbookModel
. Recall that you assigned the segue’s ID,entriessegue
, when you created the segue. Including the selectedGuestbookModel
lets Entry List Screenlet know which guestbook to display entries from:func screenlet(screenlet: GuestbookListScreenlet, onGuestbookSelected guestbook: GuestbookModel) { performSegue(withIdentifier: "entriessegue", sender: guestbook) }
-
Next, you must set the segue’s destination view controller to an
EntriesViewController
instance, and set that instance’sselectedGuestbook
variable to the selected guestbook. This ensures that Entry List Screenlet receives the guestbook you sent inperformSegue(withIdentifier:sender:)
.You do this by overriding the
prepare(for:sender:)
method. This method is called byperformSegue(withIdentifier:sender:)
before the segue occurs. Theprepare(for:sender:)
method’ssender
parameter receives the guestbook sent byperformSegue(withIdentifier:sender:)
. Currently,prepare(for:sender:)
is commented out at the bottom ofGuestbooksViewController
. Xcode created this method for you when you used the Cocoa Touch Class template to create a view controller class. Uncomment the method, and replace it with this:override func prepare(for segue: UIStoryboardSegue, sender: Any?) { guard segue.identifier == "entriessegue", let entriesViewController = segue.destination as? EntriesViewController, let selectedGuestbook = sender as? GuestbookModel else {return} entriesViewController.selectedGuestbook = selectedGuestbook }
The
guard
statement ensures that the code only runs when the segue ID isentriessegue
, the destination view controller isEntriesViewController
, and thesender
is aGuestbookModel
. In other words, the code only runs in preparation for the segue to the entries scene. The code that runs when that condition is met is only one line long:entriesViewController.selectedGuestbook = selectedGuestbook
This sets the guestbook received by
prepare(for:sender:)
to the guestbook inEntriesViewController
. As the variable names indicate, this is the guestbook the user selects in Guestbook List Screenlet. -
Now you must get a Guestbook List Screenlet reference. You’ll do this by creating an outlet to the Screenlet. Return to your storyboard and enter the Assistant editor to display
GuestbooksViewController
’s code and the storyboard side by side. With Guestbook List Screenlet selected in the storyboard, Control-drag from the Screenlet to theGuestbooksViewController
class, just below the class declaration. Release your mouse button, enter the following information in the dialog that appears, and click Connect:- Connection: Outlet
- Name: screenlet
- Type: GuestbookListScreenlet
- Storage: Weak
Xcode then adds the following code inside the
GuestbooksViewController
class:@IBOutlet weak var screenlet: GuestbookListScreenlet!
-
Use this new
screenlet
variable to set the view controller as the Screenlet’s delegate. Do this in theviewDidLoad()
method by deleting the placeholder comment and inserting this code below the call tosuper.viewDidLoad()
:self.screenlet.delegate = self
Great! The guestbooks scene now contains Guestbook List Screenlet. Next, you’ll use Entry List Screenlet in the entries scene.