Finishing the sample app

The view controllers and navigation are now in place. All we need now is to add some addition controls to view and edit information and a little logic.

Finishing DetailViewController

To finish DetailViewController, we need a set of UILabel controls that can be used to display the properties of a park, and add buttons that can initiate actions to view photos or receive directions.

To finish DetailViewController, perform the following steps:

  1. Add a UIScrollView onto the View for DetailViewController.
  2. Add UILabel controls for each property defined on NationalPark except for the Id property. Also add UILabel controls that can be used as labels for the properties. Use the screen mockups from the The sample national parks app section as a guide to lay out the controls.
  3. Enter a name for each UILabel control that will be used to display park properties so that outlets can be created.
  4. Update the ToUI() method so that the UILabel controls are populated with data from the park, as follows:
    void ToUI()
    {
      // Update the user interface for the detail item
      if (IsViewLoaded && _park != null) {
          nameLabel.Text = _park.Name;
          descriptionLabel.Text = _park.Description;
          stateLabel.Text = _park.State;
          countryLabel.Text = _park.Country;
          latitudeLabel.Text = _park.Latitude.ToString ();
          longitudeLabel.Text = _park.Longitude.ToString ();
      }
    }
  5. Add a UIButton instance with a title of photos with an action named PhotoClicked in the Touch Down event.
  6. Add an implementation for the PhotoClicked action, which opens a URL to view photos on www.bing.com that uses the park's name as the search parameter:
    partial void PhotosClicked (UIButton sender)
    {
        string encodedUriString =
           Uri.EscapeUriString(String.Format(
              "http://www.bing.com/images/search?q={0}", _park.Name));
       NSUrl url = new NSUrl(encodedUriString);
       UIApplication.SharedApplication.OpenUrl (url);
    }
  7. Add a UIButton instance with a title of directions with an action named DirectionsClicked in the Touch Down event.
  8. Add an implementation for the DirectionsClicked action, which opens a URL to receive directions to a park that uses the park's latitude and longitude coordinates:
    partial void DirectionsClicked (UIButton sender)
    {
        if ((_park.Latitude.HasValue) && (_park.Longitude.HasValue))
        {
            NSUrl url = new NSUrl (
                   String.Format(
                       "http://maps.apple.com/maps?daddr={0},{1}",
                        _park.Latitude, _park.Longitude));
    
           UIApplication.SharedApplication.OpenUrl (url);
       }
    }
  9. Add appropriate constraints to UIScrollView and and UILables so that scrolling and layout works as desired in the landscape and portrait modes. Take a look at the example for more clarity.

Finishing EditViewController

To finish EditViewController, we need to add labels and edit controls in order to edit the park data. We also need to do some data conversion and save the updates.

To finish EditViewController, perform the following steps:

  1. Add a UIScrollView instance on the View for EditViewController.
  2. Add controls to the EditViewController class along with the corresponding outlets to allow editing of each property on the NationalPark entity. The UITextField controls can be used for everything except the description property, which is better suited to a UITextView control. Also add UITextLabel controls to label properties of the park. You can again use the screen mockups from the The sample national parks app section as a guide.
  3. Update the ToUI() method to account for the new fields:
    private void ToUI ()
    {
       // Update the user interface for the detail item
       if (IsViewLoaded && _park != null) {
         nameTextField.Text = _park.Name;
         descriptionTextView.Text = _park.Description;
         stateTextField.Text = _park.State;
         countryTextField.Text = _park.State;
         latitudeTextField.Text = _park.Latitude.ToString();
         longitudeTextField.Text =
                 _park.Longitude.ToString(); 
       }
    }
  4. Create a new method that moves data from the UI controls to the entity class prior to saving it, as follows:
    void ToPark()
    {
      _park.Name = nameTextField.Text;
      _park.Description = descriptionTextView.Text;
      _park.State = stateTextField.Text;
      _park.Country = countryTextField.Text;
    
      if (String.IsNullOrEmpty (latitudeTextField.Text))
        _park.Latitude =
            Double.Parse (latitudeTextField.Text);
      else
            _park.Latitude = null;
    
      if (String.IsNullOrEmpty (longitudeTextField.Text))
        _park.Longitude =
            Double.Parse (longitudeTextField.Text);
      else
        _park.Longitude = null;
    }
  5. Update the DoneClicked() action to call ToPark() in order to move values to the park object prior to saving changes to NationalParks.json:
    partial void DoneClicked (NSObject sender)
       {
        ToPark ();
    
        . . .
    }
  6. Add appropriate constraints to UIScrollView and UITextFields so that scrolling and layout works as desired in landscape and portrait modes. Take a look at the reference solution for more clarity.
  7. Add logic to scroll the active UITextField into view when the keyboard is displayed. There are several methods of accomplishing this. Refer to the example for reference solution.

Running the app

Okay, we now have a fairly functional app. Run the app in the simulator and test each screen and navigation path. The following screenshots show the final result of the three view controllers:

Running the app
..................Content has been hidden....................

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