Hello.iOS Multiscreen Quickstart

This part of the walkthrough will add a second screen to the Phoneword application that will display a history of the phone numbers that were called with the app. The final application will have a second screen that displays the call history, as illustrated by the following screenshot:

The accompanying Deep Dive, will review the application that is build built and discuss architecture, navigation, and other new iOS concepts that we encounter along the way.

Requirements

This guide resumes where the Hello, iOS document left off, and requires completion of the Hello, iOS Quickstart. The completed version of the Phoneword app can be downloaded from the Hello, iOS sample.

Walkthrough

This walkthrough will add a Call History screen to our Phoneword application.

  1. Open the Phoneword application in Visual Studio for Mac. If necessary, the completed Phoneword application from the Hello, iOS walkthrough guide can be downloaded from here.

  2. Open the Main.storyboard file from the Solution Pad:

  1. Drag a Navigation Controller from the Toolbox onto the design surface (You might need to zoom out to fit these all on the design surface!):

  1. Drag the Sourceless Segue (that’s the gray arrow to the left of single View Controller) to the Navigation Controller to change the starting point of the application:

  1. Select the existing Root View Controller by clicking on the bottom bar, and press Delete to remove it from the design surface. Then, move the Phoneword Scene next to the Navigation Controller:

  1. Set the ViewController as the Navigation Controller’s Root View Controller. Press down the Ctrl key and click inside the Navigation Controller. A blue line should appear. Then, still holding down the Ctrl key, drag from the Navigation Controller to the Phoneword Scene and release. This is called Ctrl-dragging:

  1. From the popover, set the relationship to Root:

The ViewController is now the Navigation Controller’s Root View Controller:

  1. Double-click on the Phoneword screen’s Title bar and change the Title to Phoneword:

  1. Drag a Button from the Toolbox and place it under the Call Button. Drag the handles to make the new Button the same width as the Call Button:

  1. In the Properties Pad, change the Name of the button to CallHistoryButton and change the Title to Call History:

  1. Create the Call History screen. From the Toolbox, drag a Table View Controller onto the design surface:

  1. Next, select the Table View Controller by clicking on the black bar at the bottom of the Scene. In the Properties Pad, change the Table View Controller’s class to CallHistoryController and press Enter:

The iOS Designer will generate a custom backing class called CallHistoryController to manage this screen’s Content View Hierarchy. The CallHistoryController.cs file will appear in the Solution Pad:

  1. Double-click on the CallHistoryController.cs file to open it and replace the contents with the following code:
using System;
using Foundation;
using UIKit;
using System.Collections.Generic;

namespace Phoneword_iOS
{
    public partial class CallHistoryController : UITableViewController
    {
        public List<string> PhoneNumbers { get; set; }

        static NSString callHistoryCellId = new NSString ("CallHistoryCell");

        public CallHistoryController (IntPtr handle) : base (handle)
        {
            TableView.RegisterClassForCellReuse (typeof(UITableViewCell), callHistoryCellId);
            TableView.Source = new CallHistoryDataSource (this);
            PhoneNumbers = new List<string> ();
        }

        class CallHistoryDataSource : UITableViewSource
        {
            CallHistoryController controller;

            public CallHistoryDataSource (CallHistoryController controller)
            {
                this.controller = controller;
            }

            public override nint RowsInSection (UITableView tableView, nint section)
            {
                return controller.PhoneNumbers.Count;
            }

            public override UITableViewCell GetCell (UITableView tableView, NSIndexPath indexPath)
            {
                var cell = tableView.DequeueReusableCell (CallHistoryController.callHistoryCellId);

                int row = indexPath.Row;
                cell.TextLabel.Text = controller.PhoneNumbers [row];
                return cell;
            }
        }
    }
}

Save the application (⌘ + s) and build it (⌘ + b) to ensure there are no errors.

  1. Create a Segue (transition) between the Phoneword Scene and the Call History Scene. In the Phoneword Scene, select the Call History Button and Ctrl-drag from the Button to the Call History Scene:

From the Action Segue popover, select Show

The iOS Designer will add a Segue between the two Scenes:

  1. Add a Title to the Table View Controller by selecting the black bar at the bottom of the Scene and changing the View Controller Title to Call History in the Properties Pad:

  1. When the application is run, the Call History Button will open the Call History screen, but the Table View will be empty because there is no code to to keep track of and display the phone numbers.

This app will store the phone numbers as a list of strings.

Add a using directive for System.Collections.Generic at the top of ViewController:

using System.Collections.Generic;
  1. Modify the ViewController class with the following code:

    using System;
    using System.Collections.Generic;
    using Foundation;
    using UIKit;
    
    namespace Phoneword_iOS
    {
      public partial class ViewController : UIViewController
      {
        string translatedNumber = "";
    
        public List<string> PhoneNumbers { get; set; }
    
        protected ViewController(IntPtr handle) : base(handle)
        {
          //initialize list of phone numbers called for Call History screen
          PhoneNumbers = new List<string>();
        }
    
        public override void ViewDidLoad()
        {
          base.ViewDidLoad();
          // Perform any additional setup after loading the view, typically from a nib.
    
          TranslateButton.TouchUpInside += (object sender, EventArgs e) => {
            // Convert the phone number with text to a number
            // using PhoneTranslator.cs
            translatedNumber = PhoneTranslator.ToNumber(
              PhoneNumberText.Text);
    
            // Dismiss the keyboard if text field was tapped
            PhoneNumberText.ResignFirstResponder();
    
            if (translatedNumber == "")
            {
              CallButton.SetTitle("Call ", UIControlState.Normal);
              CallButton.Enabled = false;
            }
            else
            {
              CallButton.SetTitle("Call " + translatedNumber,
                UIControlState.Normal);
              CallButton.Enabled = true;
            }
          };
    
          CallButton.TouchUpInside += (object sender, EventArgs e) => {
    
            //Store the phone number that we're dialing in PhoneNumbers
            PhoneNumbers.Add(translatedNumber);
    
            // Use URL handler with tel: prefix to invoke Apple's Phone app...
            var url = new NSUrl("tel:" + translatedNumber);
    
            // otherwise show an alert dialog
            if (!UIApplication.SharedApplication.OpenUrl(url))
            {
              var alert = UIAlertController.Create("Not supported", "Scheme 'tel:' is not supported on this device", UIAlertControllerStyle.Alert);
              alert.AddAction(UIAlertAction.Create("Ok", UIAlertActionStyle.Default, null));
              PresentViewController(alert, true, null);
            }
          };
        }
    
        public override void PrepareForSegue(UIStoryboardSegue segue, NSObject sender)
        {
          base.PrepareForSegue(segue, sender);
    
          // set the View Controller that’s powering the screen we’re
          // transitioning to
    
          var callHistoryContoller = segue.DestinationViewController as CallHistoryController;
    
          //set the Table View Controller’s list of phone numbers to the
          // list of dialed phone numbers
    
          if (callHistoryContoller != null)
          {
            callHistoryContoller.PhoneNumbers = PhoneNumbers;
          }
        }
      }
    }
    

There are a few things happening here

  • The variable translatedNumber was moved from the ViewDidLoad method to a class-level variable.
  • The CallButton code was modified to add dialed numbers to the list of phone numbers by calling PhoneNumbers.Add(translatedNumber)
  • The PrepareForSegue method was added

Save and build the application to make sure there are no errors.

  1. Press the Start button to launch the application inside the iOS Simulator:

Congratulations on completing your first multi-screen Xamarin.iOS application!

The app can now handle navigation using both Storyboard Segues and in code. Now it’s time to dissect the tools and skills we just learned in the Hello, iOS Multiscreen Deep Dive.