You’ll use Entry List Screenlet the same way you use any Screenlet: insert it in a storyboard scene, then conform the scene’s view controller class to the Screenlet’s delegate protocol. You’ll follow these steps now to use Entry List Screenlet in the entries scene.
Adding Entry List Screenlet to the Entries Scene
Follow these steps to add Entry List Screenlet to the entries scene:
-
In your storyboard, select the entries 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. -
Resize the new view to take up all the space below the navigation bar. Then 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.
-
With the new view still selected, open the Identity inspector and set the view’s custom class to
EntryListScreenlet
. The view now appears as Entry List Screenlet in the outline view.
Great! The entries scene now contains Entry List Screenlet. Next, you’ll conform the scene’s view controller class to the Screenlet’s delegate.
Conforming to the Screenlet’s Delegate Protocol
To respond to Entry List Screenlet’s events, EntriesViewController
must
conform to the EntryListScreenletDelegate
protocol. You created this delegate
when creating the Screenlet. This delegate defines methods for responding to the
success or failure to retrieve entries, and the selection of an entry in the
list.
Follow these steps to conform EntriesViewController
to the
EntryListScreenletDelegate
protocol:
-
Import
LiferayScreens
, and in the class declaration setEntriesViewController
to adopt theEntryListScreenletDelegate
protocol. The first few lines of the class should look like this:import UIKit import LiferayScreens class EntriesViewController: UIViewController, EntryListScreenletDelegate {...
-
Implement the
EntryListScreenletDelegate
methodscreenlet(_:onEntryListResponse:)
. Recall that this method lets you respond to a successful server call. Its arguments include theEntryModel
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: EntryListScreenlet, onEntryListResponse entries: [EntryModel]) { }
-
Implement the
EntryListScreenletDelegate
methodscreenlet(_:onEntryListError:)
. Recall that this method lets you respond to a failed server call. Its arguments include the resultingNSError
object. You don’t have to do anything in this method, but it’s a good idea to print the error:func screenlet(screenlet: EntryListScreenlet, onEntryListError error: NSError) { print("Failed to retrieve guestbook entries: \(error.localizedDescription)") }
-
Implement the
EntryListScreenletDelegate
methodscreenlet(_:onEntrySelected:)
. Recall that this method lets you respond when a user selects an entry in the list. It does so by including the selectedEntryModel
object in its arguments. Since there’s currently not a scene or other action that handles detailed information about an entry, you don’t need to do anything in this method:func screenlet(screenlet: EntryListScreenlet, onEntrySelected entry: EntryModel) { }
-
Now you must get an Entry 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
EntriesViewController
’s code and the storyboard side by side. With Entry List Screenlet selected in the storyboard, Control-drag from the Screenlet to theEntriesViewController
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: EntryListScreenlet
- Storage: Weak
Xcode then adds the following code inside the
EntriesViewController
class:@IBOutlet weak var screenlet: EntryListScreenlet!
-
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
-
Next, you must set the guestbook the Screenlet retrieves entries from. To do this, set the Screenlet’s
guestbookId
property to the selected guestbook’s ID, immediately below the Screenlet’s delegate assignment in theviewDidLoad()
method:self.screenlet.guestbookId = selectedGuestbook!.guestbookId
-
Lastly, you must set the selected guestbook’s name to the navigation bar’s title. This lets the scene reflect the guestbook selection in the UI. To do this, add the following line of code at the end of the
viewDidLoad()
method:self.navigationItem.title = selectedGuestbook!.name
Your
viewDidLoad()
method should now look like this:override func viewDidLoad() { super.viewDidLoad() self.screenlet.delegate = self self.screenlet.guestbookId = selectedGuestbook!.guestbookId self.navigationItem.title = selectedGuestbook!.name }
Now you’re ready to test your handiwork. Make sure your portal containing the Guestbook portlet is running, and that the portlet contains a couple guestbooks that have entries. Then run the app and log in with your credentials. You should see the list of guestbooks displayed by Guestbook List Screenlet. Selecting a guestbook in the list takes you to the entries scene, which uses Entry List Screenlet to display a list of that guestbook’s entries. Pressing the back button returns you to the guestbooks scene, where you can select a different guestbook.
Congratulations! Now you know how to use Liferay Screens and create your own Screenlets. This opens up a world of possibilities for developing apps that leverage Liferay DXP. Although you learned a great deal in this Learning Path, there’s still more. You can customize your Screenlet’s appearance, package Screenlets and Themes for redistribution, and even add multiple actions to a Screenlet. These topics, and more, are covered in the tutorials on iOS apps with Liferay Screens.