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.
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:
UIScrollView
onto the View for DetailViewController
.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.UILabel
control that will be used to display park properties so that outlets can be created.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 (); } }
UIButton
instance with a title of photos with an action named PhotoClicked
in the Touch Down
event.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); }
UIButton
instance with a title of directions with an action named DirectionsClicked
in the Touch Down
event.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); } }
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.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:
UIScrollView
instance on the View for EditViewController
.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.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(); } }
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; }
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 (); . . . }
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.UITextField
into view when the keyboard is displayed. There are several methods of accomplishing this. Refer to the example for reference solution.