Architect 2 Sencha Docs

Using Navigation View in Mobile Apps

The Navigation view container is one of the most useful and versatile components in Sencha Touch 2. It allows you to easily create an interface where your user steps through a dynamic sequence of cards, while automatically maintaining a title bar and Back button for navigation.

This guide will show you how you can create and manipulate Navigation views within Sencha Architect to build a simple wizard-like interface. Launch a Sencha Touch project in Architect, set the URL Prefix, and save the project before following these instructions.

The completed project file for this tutorial can be found here

The most important thing to understand about Navigation View is that, unlike other card-layout components like Tab Panel, you almost never add multiple child cards into it initially. If you were to do so, the final card would be displayed by default.

Instead, you normally want to define the Navigation view with only one initial child card that displays first. Then, responding to certain user events, your app dynamically creates and pushes new child cards onto the view's navigation stack.

This means that you define each of the possible navigation cards as top-level components, independent from the Navigation view itself. Since they are created at the top level, they are defined as classes that can be instantiated individually.

It also means that you will need to use event handlers, write a small amount of JavaScript code to instantiate the cards, and push the cards onto the navigation stack. We will instantiate cards using their xtype (also called userAlias in Architect), and will use the Navigation view push method to add them to the navigation stack at the right time.

We'll start by defining each of the components that will become cards in the navigation. The first card will be called Step 1 and ask the user for a number. The second card will be called Step 2 and ask for a second number. The third card will be called Step 3 and let the user select a mathematical operator to perform a calculation.

To create the first card:

  1. Drag a form panel from the toolbox onto the canvas.
  2. Set its userClassName config to Step1Panel.
  3. Set its userAlias to step1 -- we will use this value later on when referring to the Step1 Panel component by its xtype.
  4. Add a FieldSet to the form panel.
  5. Double-click the FieldSet's title, and change it to "Enter a number".
  6. Remove one of the fields.
  7. Select the remaining field, right-click it, and choose Transform > Ext.field.Number to turn it into a number field.
  8. Set the field's name config to number1 and clear its label config.
  9. Drag a button into the form panel.
  10. Double-click the button, and set its text to Next.
  11. Set the button's ui config to forward to give it a forward-pointing look.

The second card will be very similar to the first card; rather than repeating everything from scratch we can simply right-click the Step1Panel and select Duplicate to save some time. Select the resulting duplicated component, and:

  1. Change its userClassName to Step2Panel.
  2. Change its userAlias to step2.
  3. Change its fieldset's title to Enter another number.
  4. Change its field's name to number2.

The third card is also similar to the previous two, so create a duplicate of Step2Panel, then:

  1. Change its userClassName to Step3Panel.
  2. Change its userAlias to step3.
  3. Change its fieldset's title to Choose an operation.
  4. Change the button's text to Calculate!
  5. Since it's the final step, set the button's ui config to confirm.
  6. Remove the number field from the fieldset and add four Radio Button fields in its place.
  7. Set the name config for each of the radio buttons to operation so they will function as a single radio group.
  8. Give the first radio button a label of Add, and a value of add.
  9. Give the second radio button a label of Subtract and a value of subtract.
  10. Give the third radio button a label of Multiply and a value of multiply.
  11. Give the fourth radio button a label of Divide and a value of divide.
  12. Set the checked config of the first radio to true so it will be selected by default.

Now that we've defined the cards, let's create the Navigation view that will display them.

  1. Drag a Navigation View to the top level.
  2. Set its userClassName to MainNav.
  3. Right-click the navigation view in the Inspector and select Mark as Initial View so it displays immediately when the app starts up.
  4. The navigation view should display the Step 1 card by default, so drag Step1Panel onto MainNav in the Inspector and select Link; this creates a linked instance within the navigation view pointing to the Step1Panel.
  5. Select the new linked instance and set its title config to Step 1, which will be displayed in the navigation view's title bar.

Now that the application's view is created, let's add some behavior to it.

Implement the behavior of the app by creating a controller. To add a controller to the project, click the add button ("+") at the top-right of the project Inspector and select Controller. Select the controller in the Inspector and set its userClassName to MainNavController.

Next create a controller action to handle the Next button in the Step 1 card:

  1. Select the controller, find the Actions config, and click its add button ("+") to add a new controller action.
  2. Choose Ext.Button as the target type.
  3. Choose tap as the event name.
  4. Select the new tap controller action in the Inspector.
  5. Change its fn config to onStep1SubmitTap.
  6. Change its controlQuery to step1 button. (This controlQuery is a ComponentQuery selector, which says to handle tap events for any button within the container with an xtype of step1.)
  7. Double-click the controller action in the Inspector and enter the following JavaScript code into the code editor:

      button.up('navigationview').push({
          xtype: 'step2',
          title: 'Step 2'
      });
    

This code says to find the navigationview ancestor of the button, and to push a new Step2Panel card instance onto the end of the navigation stack, automatically animating it into view. We refer to the Step2Panel component class by its xtype which we assigned earlier by setting the Step2Panel's userAlias. We also give the new step2 instance a title of "Step 2" which will be displayed in the titlebar when this card is visible.

The controller action for the Step 2 button should be nearly identical. Follow the same steps as for the Step 1 button action, but:

  1. Set the controller action's fn config to onStep2ButtonTap.
  2. Set its controlQuery to step2 button.
  3. Change its handler code so it refers to Step 3 instead:

     button.up('navigationview').push({
         xtype: 'step3',
         title: 'Step 3'
     });
    

Follow the same steps to create a controller action for the Step 3 button, but:

  1. Set its fn to onStep3ButtonTap.
  2. Set its controlQuery to step3 button.
  3. For the handler code, double click the controller action in the Inspector and insert the following using the code editor:

      var mainNav = button.up('navigationview'),
          num1 = mainNav.child('step1').getValues().number1,
          num2 = mainNav.child('step2').getValues().number2,
          operation = mainNav.child('step3').getValues().operation,
          result;
    
      switch (operation) {
          case 'add':
              result = num1 + num2;
              break;
          case 'subtract':
              result = num1 - num2;
              break;
          case 'multiply':
              result = num1 * num2;
              break;
          case 'divide':
              result = num1 / num2;
              break;
      }
    
      Ext.Msg.alert('Your result is: ' + result);
    

In a nutshell, this code finds the forms for each step and asks for the values of their fields by name. It then performs the mathematical calculation by applying the chosen operator to the two entered numbers and displays the result in an alert popup.

The application is completely functional. Save and preview the project to see it in action in your WebKit-based desktop browser or on your mobile device. Notice that as you progress through the steps, the Navigation View's title bar is automatically updated to display the title of the current card, and the Back button is automatically updated to allow you to go back to the previous step.

The Navigation view also supports some other useful options. For example, go to Architect and select the Navigation view and set its useTitleForBackButtonText config to true. Save and preview again and notice that the Back button now displays the title of the previous card rather than the static Back label.

You can also control the title bar at the top of the Navigation view by selecting it in the Inspector, finding its Navigation Bar item in Config, and click the add button. You can then select and configure the Navigation bar child, add additional items into it, and so on.

Now you have an understanding of how to create and manipulate Sencha Touch 2 Navigation View containers using Sencha Architect. You will find this very useful when creating common sequential UI navigation structures in your Touch apps.