Configuring the SubscribeController View

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

  1. Click the SubscribeController.xib file to reveal a view of the (presently empty) screen in the right pane and the following three items represented by icons in the middle pane:
    • 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. Select the File's Owner icon, select View > Utilities > Identity Inspector, and select SubscribeController in the Class field.
    This tells Interface Builder the class of the object to allow you to make connections to and from the File's Owner.
  3. Select the View icon, and in the Identity Inspector panel, select UIView in the Class field.
  4. Select View > Utilities > Object Library. In the Object Library panel, select the Round Rect Button item, drag it onto the view, and then double-click it and enter Subscribe and press Return.
  5. In the Accessibility section of the Identity Inspector, temporarily uncheck Enabled.
    We temporarily disable the button because the application cannot subscribe to the server for updates until it is connected.
  6. Click the SubscribeController.h file and replace the existing code with the provided code to make connections to the user interface from the view controller, by specifying outlets and adding property declarations for the instance variables and a declaration for the action method:
    #import <UIKit/UIKit.h>
    @class CustomerListController;
    
    @interface SubscribeController : UIViewController {
    }
    
    -(IBAction)buttonPressed:(id)sender;
    
    @property (nonatomic, retain) IBOutlet UIButton *button;
    @property (nonatomic, retain) CustomerListController *listController;
    
    @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 opens.
  7. Click the SubscribeController.m file and replace the existing code with the provided code.
    #import "SubscribeController.h"
    #import "SUP101_SUP101DB.h"
    #import "SUP101CallbackHandler.h"
    #import "Sup101AppDelegate.h"
    #import "CustomerListController.h"
    
    @implementation SubscribeController
    
    @synthesize button, listController;
    
    - (void) showListController {
        SUP101AppDelegate *delegate = [[UIApplication sharedApplication] delegate];
    	[delegate.navController pushViewController:self.listController animated:YES];
    }
    
    - (void) onImportSuccess:(NSNotification *)object {
        // We have data, so present the view that will display it.
        self.listController = [[CustomerListController alloc] initWithStyle:UITableViewStylePlain];
        [self showListController];
        [[NSNotificationCenter defaultCenter] removeObserver:self name:ON_IMPORT_SUCCESS object:nil];
    }
    
    - (IBAction)buttonPressed:(id)sender
    {
        if (self.listController != nil) {
            [self showListController];
        } else {
            // Although there is an onSubscribeSuccess notification, data has not arrived on the device until the server sends
            // onImportSuccess. Don't block the UI thread while waiting -- always listen for a notification.
            [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onImportSuccess:) name:ON_IMPORT_SUCCESS object:nil];
            [SUP101_SUP101DB subscribe];
        }
    }
    
    /*
     // 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;
     }
     */
    
    - (void)onLoginSuccess:(NSNotification *)notification {
        self.button.enabled = YES;
        [[NSNotificationCenter defaultCenter] removeObserver:self name:ON_LOGIN_SUCCESS object:nil];
    }
    
    - (void)viewDidAppear:(BOOL)animated {
        [super viewDidAppear:animated];
        
        if (self.listController == nil) {
            // The application cannot subscribe to data updates until login has completed. Wait for an ON_LOGIN_SUCCESS notification
            // to arrive before enabling the 'Subscribe' button
            [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onLoginSuccess:) name:ON_LOGIN_SUCCESS object:nil];
        } else {
            // Already subscribed, and a list controller has been created. Just show it again.
            [self.button setTitle:@"Show List" forState:UIControlStateNormal];
        }
    }
    
    - (void)viewDidDisappear:(BOOL)animated {
        [super viewDidDisappear:animated];
        [[NSNotificationCenter defaultCenter] removeObserver: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 {
        self.listController = nil;
        [super dealloc];
    }
    
    
    @end
    
  8. Click the SubscribeController.xib file and control-click the Subscribe button to show the inspector.
  9. Drag from the circle to the right of Touch Up Inside to the File's Owner icon and release, then click on buttonPressed to establish a connection between the Subscribe button and the button's action method:

    iphone_subscribe_button_connect