Goal: Use Interface Builder to configure the SubscribeController.xib file and create the user interface.
#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
#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