iOS     Programming:




navigation-based applications
stop 1: Navigation-Based App
        and NSMutableArray
1.1. Create a New project
1.1.UITableViewController




Window       NavigationController   UITableViewController
1.1.UITableViewController




                        UITableViewDataSource   UITableViewDelegate




UITableViewController
UITableViewDataSource

  – tableView:cellForRowAtIndexPath:

  - tableView:numberOfRowsInSection: 

  - tableView:titleForHeaderInSection:

  - numberOfSectionsInTableView:


             UITableViewDelegate

– tableView: didSelectRowAtIndexPath:

- tableView: heightForHeaderInSection

- tableView: viewForHeaderInSection
RootViewController.h
                               NSMutableArray




//     RootViewController.h


#import <UIKit/UIKit.h>

@interface RootViewController : UITableViewController {

       NSMutableArray* data;
}

@property (nonatomic, retain) NSMutableArray* data;

@end
RootViewController.m
                                NSMutableArray




//   RootViewController.m

- (void)viewDidLoad {

     [super viewDidLoad];

     NSMutableArray* tmpArray =
                 [[NSMutableArray alloc]
                 initWithObjects:@”one”, @”two”, @”three”,nil];

     self.data = tmpArray;

     [tmpArray release];
}
RootViewController.m
                            TableViewDataSource




//   RootViewController.m

// Customize the number of sections in the table view.

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView
             numberOfRowsInSection:(NSInteger)section {
    return [self.data count];
}
RootViewController.m
                                    TableViewDataSource

    //     RootViewController.m

    // Customize the appearance of table view cells.

    - (UITableViewCell *)tableView:(UITableView *)tableView
                         cellForRowAtIndexPath:(NSIndexPath *)indexPath {

         static NSString *CellIdentifier = @"Cell";

         UITableViewCell *cell = [tableView
                                  dequeueReusableCellWithIdentifier:CellIdentifier];
         if (cell == nil) {
             cell = [[[UITableViewCell alloc]
                     initWithStyle:UITableViewCellStyleDefault
                     reuseIdentifier:CellIdentifier] autorelease];
         }

     // Configure the cell.
     cell.textLabel.text = [self.data objectAtIndex:indexPath.row];
     return cell;

}
RootViewController.m
                            TableViewDataSource




//   RootViewController.m


#import "RootViewController.h"


@implementation RootViewController

@synthesize data;

- (void)dealloc {
    [data release];
    [super viewDidLoad];
}
Test Drive
 version 1.0
Test Drive
what if we have a lot information...
stop 2: Adding Views for Details
       and plist
plist file
(1) plist - initialize



//   RootViewController.m


- (void)viewDidLoad {

     [super viewDidLoad];

     NSString *path = [[NSBundle mainBundle]
                       pathForResource:@"data" ofType:@"plist"];

     NSMutableArray* tmpArray = [[NSMutableArray alloc]
                              initWithContentsOfFile:path];
     self.data = tmpArray;

     [tmpArray release];
}
UIViewController
file.xib
file.h



//     DetailViewController.h


#import <UIKit/UIKit.h>


@interface DetailViewController : UIViewController {

     IBOutlet UILabel *label;

}

@property (nonatomic, retain) UILabel *label;

@end
file.m



//   DetailViewController.m


#import "DetailViewController.h"


@implementation DetailViewController

@synthesize label, text;


- (void)dealloc {

     [super dealloc];
     [label release];
     [text release];

}
                                        and connect the
                                            outlet
stop 3: Link Table and Detail
RootViewController.m

//   RootViewController.m

#import "RootViewController.h"

#import "DetailViewController.h"




- (void)tableView:(UITableView *)tableView
                    didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

     DetailViewController *detailViewController =
        [[DetailViewController alloc]
        initWithNibName:@"DetailViewController"
        bundle:nil];

     [self.navigationController
        pushViewController:detailViewController
        animated:YES];

     [detailViewController release];

}
Now, we have two views!
more and more data...
stop 4: Dictionaries
        and plist
dictionary.plist
RootViewController.m
               create cell from dictionary
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView
                    cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

     UITableViewCell *cell =
              [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
     if (cell == nil) {
         cell = [[[UITableViewCell alloc]
              initWithStyle:UITableViewCellStyleDefault
              reuseIdentifier:CellIdentifier]
              autorelease];
        }

     // Configure the cell.
     // cell.textLabel.text =
     //    [self.data objectAtIndex:indexPath.row];

       cell.textLabel.text =
                 [[self.data objectAtIndex:indexPath.row] objectForKey:@"title"]

    return cell;
}
RootViewController.m

//   RootViewController.m

#import "RootViewController.h"

#import "DetailViewController.h"



- (void)tableView:(UITableView *)tableView
                    didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

     DetailViewController *detailViewController =
        [[DetailViewController alloc]
        initWithNibName:@"DetailViewController"
        bundle:nil];

     [self.navigationController
        pushViewController:detailViewController
        animated:YES];

     detailViewController.row = [self.data objectAtIndex:indexPath.row];

     [detailViewController release];

}
DetailView


NSDictionary *drink;

@property (nonatomic, retain) NSDictionary *drink;

@synthesize row

[drink release];



      - (void) viewWillAppear: (BOOL)animated {
         [super viewWillAppear:animated];

         label.text = [row objectForKey:@"title"];
         text.text = [drink objectForKey:@"desc"];
  }
stop 5: User Experince
        with UITableView
Human Interface Guide
RootViewController.m
 tableView cellForRowAtIndexPath:

cell = [[[UITableViewCell alloc]
initWithStyle: UITableViewCellStyleSubtitle
reuseIdentifier:CellIdentifier] autorelease];

cell.textLabel.text = [
[self.data objectAtIndex:indexPath.row]
objectForKey:@"title"];


cell.detailTextLabel.text = [
[self.data objectAtIndex:indexPath.row]
objectForKey:@"desc"];
RootViewController.m
 tableView cellForRowAtIndexPath:

cell.imageView.image =
[UIImage imageNamed:@"anyFileName”];


cell.accessoryType =
UITableViewCellAccessoryDisclosureIndicator;
stop 6: Table Group
RootViewController.m
                     Table View - Grouped
// number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)
section {
! if (section == 0) return 2;
! if (section == 1) return 3;
! if (section == 2) return 1;
! else return 0;
}

// section
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:
(NSInteger)section {
! NSString *title = nil;
! switch (section) {
! ! case 0: title = @"Behavioral patterns"; break;
! ! case 1: title = @"Creational patterns"; break;       XIB file:
! ! case 2: title = @"Structural patterns"; break;
! ! default: break;                                      style = grouped
! }
! return title;
}

// number of section
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 3;
}
stop 7: iAds
ExampleAppDelegate.h
                           Adding a common iAds Banner




//     MuralistasAppDelegate.h

#import <UIKit/UIKit.h>

#import <iAd/iAd.h>
#define SharedAdBannerView
((MuralistasAppDelegate *)[[UIApplication sharedApplication] delegate]).banner

@interface MuralistasAppDelegate : NSObject <UIApplicationDelegate> {
   ADBannerView *banner;!
}

@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet UINavigationController *navigationController;
@property (nonatomic, retain) ADBannerView *banner;

@end
ExampleAppDelegate.m
                            Adding a common iAds Banner


    // didFinishLaunchingWithOptions

    - (BOOL) application:(UIApplication *)application
             didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

        // Add the navigation controller's view to the window and display.
        self.window.rootViewController = self.navigationController;
        [self.window makeKeyAndVisible];

        // Banner
        banner = [[ADBannerView alloc] initWithFrame:CGRectZero];

         // banner is pinned to the bottom
    !   self.banner.autoresizingMask =
            UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight |
            UIViewAutoresizingFlexibleTopMargin;

    !   // Supported landscape or portrait
        [NSSet setWithObjects: ADBannerContentSizeIdentifier320x50,
        ADBannerContentSizeIdentifier480x32, nil];
!
        return YES;
}
<ADBannerViewDelegate>
       iAds Banner




  Add iAds Delegate Methods

               :)
<ADBannerViewDelegate>
       iAds Banner
.javiergs.com

201104 iphone navigation-based apps

  • 1.
    iOS Programming: navigation-based applications
  • 2.
    stop 1: Navigation-BasedApp and NSMutableArray
  • 3.
    1.1. Create aNew project
  • 4.
    1.1.UITableViewController Window NavigationController UITableViewController
  • 5.
    1.1.UITableViewController UITableViewDataSource UITableViewDelegate UITableViewController
  • 6.
    UITableViewDataSource – tableView:cellForRowAtIndexPath: - tableView:numberOfRowsInSection:  - tableView:titleForHeaderInSection: - numberOfSectionsInTableView: UITableViewDelegate – tableView: didSelectRowAtIndexPath: - tableView: heightForHeaderInSection - tableView: viewForHeaderInSection
  • 7.
    RootViewController.h NSMutableArray // RootViewController.h #import <UIKit/UIKit.h> @interface RootViewController : UITableViewController { NSMutableArray* data; } @property (nonatomic, retain) NSMutableArray* data; @end
  • 8.
    RootViewController.m NSMutableArray // RootViewController.m - (void)viewDidLoad { [super viewDidLoad]; NSMutableArray* tmpArray = [[NSMutableArray alloc] initWithObjects:@”one”, @”two”, @”three”,nil]; self.data = tmpArray; [tmpArray release]; }
  • 9.
    RootViewController.m TableViewDataSource // RootViewController.m // Customize the number of sections in the table view. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [self.data count]; }
  • 10.
    RootViewController.m TableViewDataSource // RootViewController.m // Customize the appearance of table view cells. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; } // Configure the cell. cell.textLabel.text = [self.data objectAtIndex:indexPath.row]; return cell; }
  • 11.
    RootViewController.m TableViewDataSource // RootViewController.m #import "RootViewController.h" @implementation RootViewController @synthesize data; - (void)dealloc { [data release]; [super viewDidLoad]; }
  • 12.
  • 13.
    Test Drive what ifwe have a lot information...
  • 14.
    stop 2: AddingViews for Details and plist
  • 15.
  • 16.
    (1) plist -initialize // RootViewController.m - (void)viewDidLoad { [super viewDidLoad]; NSString *path = [[NSBundle mainBundle] pathForResource:@"data" ofType:@"plist"]; NSMutableArray* tmpArray = [[NSMutableArray alloc] initWithContentsOfFile:path]; self.data = tmpArray; [tmpArray release]; }
  • 17.
  • 18.
  • 19.
    file.h // DetailViewController.h #import <UIKit/UIKit.h> @interface DetailViewController : UIViewController { IBOutlet UILabel *label; } @property (nonatomic, retain) UILabel *label; @end
  • 20.
    file.m // DetailViewController.m #import "DetailViewController.h" @implementation DetailViewController @synthesize label, text; - (void)dealloc { [super dealloc]; [label release]; [text release]; } and connect the outlet
  • 21.
    stop 3: LinkTable and Detail
  • 22.
    RootViewController.m // RootViewController.m #import "RootViewController.h" #import "DetailViewController.h" - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { DetailViewController *detailViewController = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil]; [self.navigationController pushViewController:detailViewController animated:YES]; [detailViewController release]; }
  • 23.
    Now, we havetwo views!
  • 24.
  • 25.
  • 26.
  • 27.
    RootViewController.m create cell from dictionary // Customize the appearance of table view cells. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; } // Configure the cell. // cell.textLabel.text = // [self.data objectAtIndex:indexPath.row]; cell.textLabel.text = [[self.data objectAtIndex:indexPath.row] objectForKey:@"title"] return cell; }
  • 28.
    RootViewController.m // RootViewController.m #import "RootViewController.h" #import "DetailViewController.h" - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { DetailViewController *detailViewController = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil]; [self.navigationController pushViewController:detailViewController animated:YES]; detailViewController.row = [self.data objectAtIndex:indexPath.row]; [detailViewController release]; }
  • 29.
    DetailView NSDictionary *drink; @property (nonatomic,retain) NSDictionary *drink; @synthesize row [drink release]; - (void) viewWillAppear: (BOOL)animated { [super viewWillAppear:animated]; label.text = [row objectForKey:@"title"]; text.text = [drink objectForKey:@"desc"]; }
  • 30.
    stop 5: UserExperince with UITableView
  • 31.
  • 32.
    RootViewController.m tableView cellForRowAtIndexPath: cell= [[[UITableViewCell alloc] initWithStyle: UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease]; cell.textLabel.text = [ [self.data objectAtIndex:indexPath.row] objectForKey:@"title"]; cell.detailTextLabel.text = [ [self.data objectAtIndex:indexPath.row] objectForKey:@"desc"];
  • 33.
    RootViewController.m tableView cellForRowAtIndexPath: cell.imageView.image= [UIImage imageNamed:@"anyFileName”]; cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
  • 34.
  • 35.
    RootViewController.m Table View - Grouped // number of rows in the table view. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger) section { ! if (section == 0) return 2; ! if (section == 1) return 3; ! if (section == 2) return 1; ! else return 0; } // section - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection: (NSInteger)section { ! NSString *title = nil; ! switch (section) { ! ! case 0: title = @"Behavioral patterns"; break; ! ! case 1: title = @"Creational patterns"; break; XIB file: ! ! case 2: title = @"Structural patterns"; break; ! ! default: break; style = grouped ! } ! return title; } // number of section - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 3; }
  • 36.
  • 37.
    ExampleAppDelegate.h Adding a common iAds Banner // MuralistasAppDelegate.h #import <UIKit/UIKit.h> #import <iAd/iAd.h> #define SharedAdBannerView ((MuralistasAppDelegate *)[[UIApplication sharedApplication] delegate]).banner @interface MuralistasAppDelegate : NSObject <UIApplicationDelegate> { ADBannerView *banner;! } @property (nonatomic, retain) IBOutlet UIWindow *window; @property (nonatomic, retain) IBOutlet UINavigationController *navigationController; @property (nonatomic, retain) ADBannerView *banner; @end
  • 38.
    ExampleAppDelegate.m Adding a common iAds Banner // didFinishLaunchingWithOptions - (BOOL) application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Add the navigation controller's view to the window and display. self.window.rootViewController = self.navigationController; [self.window makeKeyAndVisible]; // Banner banner = [[ADBannerView alloc] initWithFrame:CGRectZero]; // banner is pinned to the bottom ! self.banner.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleTopMargin; ! // Supported landscape or portrait [NSSet setWithObjects: ADBannerContentSizeIdentifier320x50, ADBannerContentSizeIdentifier480x32, nil]; ! return YES; }
  • 39.
    <ADBannerViewDelegate> iAds Banner Add iAds Delegate Methods :)
  • 40.
  • 41.