Chapter    14

A Swift iPhone App

In Chapter 8, you created a basic bookstore iPhone app with Swift. In this chapter, you will add some features to the app to make it a bit more functional and use many of the technologies you have learned in this book, such as creating a class, using delegates and protocols, and using actions and outlets. You’ll also learn about some new techniques such as switches, UIAlertViewController, and landmarks.

Let’s Get Started

The bookstore example in Chapter 8 enabled you to view books in your bookstore in a TableView and then tap the book to see its details. In this chapter, you will add the following capabilities to the Chapter 8 bookstore app:

  • Adding a book
  • Deleting a book
  • Modifying a book

See Figures 14-1 and 14-2.

9781484214893_Fig14-01.jpg

Figure 14-1. Add book functionality

9781484214893_Fig14-02.jpg

Figure 14-2. Adding edit and delete functionality along with using a UISwitch

Using the app you created in Chapter 8, add a Button Bar item by dragging the Button Bar Item object to the right button bar location in the Main.storyboard file. Change the Button Bar item’s title to Add. This will change the button bar’s title to Add, as shown in Figure 14-3.

9781484214893_Fig14-03.jpg

Figure 14-3. Adding a Button Bar item to your view

Modify and add the code that will handle a showDetail method and a addBookSegue segue in the MasterViewController.swift file, starting at line 51 in Listing 14-1. The code will transition to the scene that will add a book to the list and pass the view to a delegate. The next step is to define the AddBookViewController.

Note  Something new in Swift is on line 40: "// MARK: - Segues". // MARK: is called a landmark. It is replacement of the pragma mark, which is used in Objective-C. Landmarks help break up the code in the jump bar and enable you to quickly get to sections of code indicated by the landmark. When you type something following // MARK:, Xcode places the landmark in the jump bar’s drop-down, as shown in Figure 14-4. If you just type // MARK: -, Xcode adds a line separator in the jump bar drop-down. Swift also supports // TODO: and // FIXME: landmarks to annotate your code and lists them in the jump bar.

9781484214893_Fig14-04.jpg

Figure 14-4. Swift’s new landmarks

Now add the new view controller AddBookViewController mentioned in line 52 in Listing 14-1. Add a View Controller object to the storyboard by dragging a View Controller to the Main.storyboard file. Then add the objects in Figure 14-5 to enable the user to add a new book. Feel free to move the scenes around to make it clear how they relate to each other, as shown in Figure 14-5.

9781484214893_Fig14-05.jpg

Figure 14-5. Adding the AddBookViewController and objects

Add a Push Segue object from the Add Button Bar item to the new View Controller by Control-dragging dragging or right-clicking and dragging from the Add Button Bar item to the new View Controller, as shown Figure 14-6.

9781484214893_Fig14-06.jpg

Figure 14-6. Add a Show Segue object to the new View Controller

Label the Segue object by clicking the segue arrow and labeling the identifier as addBookSegue, as shown in Figure 14-7.

9781484214893_Fig14-07.jpg

Figure 14-7. Naming the Segue object addBookSegue

Now you need to create a Swift class to go with the new View Controller. Create a new file and Cocoa class and name it AddBookViewController, as shown in Figure 14-8. Make sure you select a subclass of UIViewController.

9781484214893_Fig14-08.jpg

Figure 14-8. Adding the AddBookViewController class

Now you have to associate the new AddBookViewController class to the new View Controller. Select the View Controller, and in the Identity Inspector, type AddBookViewController for the class, as shown in Figure 14-9.

9781484214893_Fig14-09.jpg

Figure 14-9. Associating the AddBookViewController class to the new View Controller

Set the title of the view to Add Book by double-clicking on the Navigation Bar. Open the AddBookViewController.swift file and add the code shown in Listing 14-2.

To the Book class, add two properties: pages and readThisBook. These are shown in lines 15 and 16 in Listing 14-3.

Switches

Connect the outlets in the AddBookViewController class by dragging them from their open circles to the controls, as shown in Figure 14-10.

9781484214893_Fig14-10.jpg

Figure 14-10. Connecting the outlets

Connect the saveBookAction action by dragging the outlet circle to the Save Book button, as shown in Figure 14-11.

9781484214893_Fig14-11.jpg

Figure 14-11. Connecting the saveBookAction

In the DetailViewController class, add the code shown in Listing 14-4.

Alert View Controllers

Add the controls for Pages, Read, and Edit for the DetailViewController. Connect the outlets by dragging the open circles to their controls, as shown in Figure 14-12.

9781484214893_Fig14-12.jpg

Figure 14-12. Adding the Pages and Read outlets

The Read switch is disabled in this view by unchecking the Enabled property in the Attributes Inspector.

Add the code for displaying an AlertViewController when the Delete Button Bar is tapped, as shown in Listing 14-5.

Add the Delete Button Bar item to the right navigation location and connect it to the action, as shown in Figure 14-13.

9781484214893_Fig14-13.jpg

Figure 14-13. Adding the Delete Right Button Bar item and action

The UIAlertViewController will warn the user that the book currently displayed in the DetailViewController is about to be deleted and will enable the user to decide whether to delete it. The UIAlertViewController has two buttons: Yes and No. When the user taps the Delete right Button Bar item, the UIAlertViewController will be as shown, in Figure 14-14, when you are done.

9781484214893_Fig14-14.jpg

Figure 14-14. UIAlertViewController being displayed

When the user taps Yes to delete the book, you want to call a deleteBook delegate method as described in the MasterViewController class. You add the delegate property that will store the MasterViewController view in Listing 14-6.

Let’s now talk about the three delegate methods: newBook, deleteBook, and editBook, as defined in the AddBookViewController class in Listing 14-2 (lines 11 to 15). Add these three functions at the end MasterViewController class, as shown in Listing 14-7.

The function newBook adds a new book to the bookstore; appending the array with the newBook does this, as shown in line 93. Line 94 then reloads the Table view by calling all the Table view delegate methods:

numberOfSectionsInTableView
numberOfRowsInSection
cellForRowAtIndexPath

Finally, you pop the DetailViewController from the navigation stack by calling popToRootViewControllerAnimated(true). Popping the view from the navigation stack means the view is removed similarly to tapping the Back button.

The function deleteBook removes the book from the bookStore array. First you determine which row was selected in the tableView and use that index to delete the book in the array by calling removeAtIndex(row!), as shown on line 102.

The function editBook enables the user to edit an existing book in the bookStore array. To do this, the function inserts the edited book in the array at the row that was selected, as shown on line 111. Then the function deletes the original book that was pushed down one index when you inserted the book in the array, as shown on line 112.

Now add the Edit button to the bottom of the DetailViewController and add a Show Segue object from the edit button to the AddBookViewController, as shown in Figure 14-15.

9781484214893_Fig14-15.jpg

Figure 14-15. Adding the Segue object

Select the Segue object you just created, select the Attributes Inspector, and name the identifier editDetail. See Figure 14-16.

9781484214893_Fig14-16.jpg

Figure 14-16. Naming the Segue’s identifier

Add the prepareForSegue function shown in Listing 14-8 to the bottom of the DetailViewController.swift file.

Finally, modify the configureView function in the DetailViewController to properly populate the pages and switch outlets, as shown in Listing 14-9.

App Summary

Compile and run the app. You should set breakpoints at the delegate functions to watch the program flow. It is a great app to see how delegates can be used to pass information from one view to another.

Additionally, you can add functionality to the app to make the information persistent by using Core Data or NSUserDefaults.

Exercises

  • Add more books to the bookstore using the original program as a guide.
  • Enhance the Book class so it can store another attribute—a price or ISBN, for example.
  • Add persistence to the app by using Core Data or NSUserDefaults.
..................Content has been hidden....................

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