Adding an unwind action method to the Done button

In Chapter 10, Building Your App Structure in Storyboard, you added an unwind action method for the Cancel button in ExploreViewController, which dismisses the Locations screen when it is tapped. Now, you'll add an unwind action method for the Done button, which dismisses the Locations screen and sets the selectedCity property in ExploreViewController to the location selected by the user. Perform the following steps:

  1. Add the following just below the unwindLocationCancel(segue:) method:
@IBAction func unwindLocationDone(segue:UIStoryboardSegue){
if let viewController = segue.source as? LocationViewController {
selectedCity = viewController.selectedCity
if let location = selectedCity {
headerView.lblLocation.text = location.full
}
}
}

This is the unwind action method that will be assigned later to the Done button in the Locations screen. The source view controller in this case is LocationViewController and the destination view controller is ExploreViewControllerLet's see how it works.

The first line of this method checks whether the source view controller is LocationViewController. If true, the selectedCity property of ExploreViewController is set to the LocationItem instance stored in the selectedCity property of LocationViewController.

The next line assigns selectedCity to a temporary constant, location, and if successful, the full property of the LocationItem instance (a string containing the name of the city and the state) is assigned to the text property of the lblLocation property of headerView.

  1. Modify the collectionView(_:viewForSupplementaryElementOfKind:at:) method as follows:
func collectionView(_ collectionView: UICollectionView, 
viewForSupplementaryElementOfKind kind: String, at indexPath:
IndexPath) -> UICollectionReusableView {
let header = collectionView.dequeueReusableSupplementaryView
(ofKind: kind, withReuseIdentifier: "header", for: indexPath)
headerView = header as? ExploreHeaderView
return headerView
}

This method is one of the data source methods declared in the UICollectionViewDataSource protocol. It returns the view that will be used in the collection view section header. Here, the collection view section header in the Explore screen is set to ExploreHeaderView.

  1. Click Main.storyboard. Look for the Location View Controller Scene. Ctrl + Drag from the Done button to the Exit icon in the Scene Dock:

  1. Choose unwindLocationDoneWithSegue: from the pop-up menu. This links the Done button with the unwindLocationDone(segue:) unwind action in ExploreViewController:


Build and run your app and tap the LOCATION button. Tap a city and a tick will appear in the row. Tap Done

The selected city name and state will replace the PLEASE SELECT A LOCATION text in the subtitle label inside the collection view section header:

Although this works, there are two issues that you need to fix when selecting a location. The first issue is that you can select multiple locations:

You want the user to only select one location, and if another is selected, the location selected earlier should be deselected.

The second issue is that the checkmark next to the user-selected location disappears if you click Done in the Locations screen and click the LOCATION button in the Explore screen again:

The last selected location should have a checkmark when you go back to the Locations screen.

Let's fix the first issue in the next section, which is to make sure only one location can be selected at a time.

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

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