Configuring the SubscribeController View

Goal: Use Interface Builder to configure the SubscribeController.xib file and create the user interface.

  1. Double-click the SubscribeController.xib file to open Interface Builder.
    The file contains three objects:
    • File's Owner – the object that is set to be the owner of the user interface, which is typically the object that loads the interface. In this tutorial, this is the SubscribeController.
    • First Responder – the first responder proxy object handles events. Connecting an action to the first responder means that when the action is invoked, it is dynamically sent to the responder chain.
    • View – displayed in a separate window to allow you to edit it.
  2. To make connections to and from the File's Owner, you must use the Identity Inspector to tell Interface Builder the class of the object:
    1. In the SubscribeController.xib document window, select the File's Owner icon, then select Tools > Identity Inspector.
    2. In the Class field, select SubscribeController.
    3. Select the View icon, then select Tools > Identity Inspector.
    4. In the Identity Inspector Class field, select UIView.
  3. Add the user interface elements to the View. In this case, you will be adding a button.
    1. In Interface Builder, select Tools > Library.
    2. Scroll through the Library and select the Button icon, then drag and drop it onto the View window.
      You can resize the button using the resize handles and position the button by dragging it to the desired location.
    3. Double-click inside the button and type: Subscribe.
  4. To make connections to the user interface from the view controller, you must specify outlets in the SubscribeController.h file. You must also add property declarations for the instance variables and a declaration for the action method:
    1. Open the SubscribeController.h file and add this code:
      #import <UIKit/UIKit.h>
      #import "CustomerListController.h"
      
      @interface SubscribeController : UIViewController {
          CustomerListController *listController;
      }
      -(IBAction)buttonPressed:(id)sender;
      @end
      
      Note: This code references a view controller (CustomerListController) you will create later in this tutorial. This code says that when the user touches the Subscribe button, the CustomerList view is called.
    2. Save the SubscribeController.h file.
  5. In the SubscribeController.m file, add the implementation code:
    1. Open the SubscribeController.m file and add:
      #import "SubscribeController.h"
      #import "SUP101_SUP101DB.h"
      #import "SUP101CallbackHandler.h"
      #import "SUP101AppDelegate.h"
      
      @implementation SubscribeController
      
      - (IBAction)buttonPressed:(id)sender
      {
          
          [SUP101_SUP101DB subscribe];
          while ([(SUP101CallbackHandler *)[SUP101_SUP101DB callbackHandler] importSuccessCount] < 1)
          {
              sleep(1);
          }
          if (listController == nil)
          {
              listController = [[CustomerListController alloc] initWithStyle:UITableViewStylePlain];
          }    
          SUP101AppDelegate *delegate = [[UIApplication sharedApplication] delegate];
      	[delegate.navController pushViewController:listController animated:YES];
      }
      
      /*
       // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
       - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
       if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
       // Custom initialization
       }
       return self;
       }
       */
      
      
      // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
      - (void)viewDidLoad {
          self.title = @"Subscribe";
          [super viewDidLoad];
      }
      
      
      /*
       // Override to allow orientations other than the default portrait orientation.
       - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
       // Return YES for supported orientations
       return (interfaceOrientation == UIInterfaceOrientationPortrait);
       }
       */
      
      - (void)didReceiveMemoryWarning {
      	// Releases the view if it doesn't have a superview.
          [super didReceiveMemoryWarning];
      	
      	// Release any cached data, images, etc that aren't in use.
      }
      
      - (void)viewDidUnload {
      	// Release any retained subviews of the main view.
      	// e.g. self.myOutlet = nil;
      }
      
      
      - (void)dealloc {
          if (listController)
          {
              [listController release];
              listController = nil;
          }
          [super dealloc];
      }
      
      
      @end
      
    2. Save the SubscribeController.m file.