Wednesday, 20 April 2011

UINavigationController Programmatically





 //navigAppdelegate.h
#import <UIKit/UIKit.h>
@class RootViewController;
@interface navigAppDelegate : NSObject <UIApplicationDelegate>
 {
   UIWindow *window;
   UINavigationController *navigationController;
    RootViewController *viewcontroller;
}
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet UINavigationController *navigationController;
@property (nonatomic, retain) IBOutlet RootViewController *viewcontroller;
@end





//navigAppdelegate.m

#import "navigAppDelegate.h"
#import "RootViewController.h"
@implementation navigAppDelegate
@synthesize window;
@synthesize navigationController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
 {
RootViewController *mainMenuVC = [[RootViewController alloc] init];
navigationController = [[UINavigationController alloc] initWithRootViewController:mainMenuVC];         [self.window addSubview:navigationController.view];
[self.window makeKeyAndVisible];
return YES;
[mainMenuVC release];
}
- (void)dealloc
 {
    [navigationController release];
    [window release];
    [super dealloc];
}
@end


#import <UIKit/UIKit.h>

@class first;

@class second;

@class third;

@interface RootViewController : UITableViewController
 {
   
first *view1;
   
second *view2;
   
third *view3;

}
@property (nonatomic, retain) IBOutlet first *view1;

@property (nonatomic, retain) IBOutlet second *view2;

@property (nonatomic, retain) IBOutlet third *view3;

@end





//RootViewController.m

#import "navigAppDelegate.h"
#import "RootViewController.h"
@implementation navigAppDelegate
@synthesize window;
@synthesize navigationController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
 {   
 RootViewController *mainMenuVC = [[RootViewController alloc] init];
 navigationController = [[UINavigationController alloc] initWithRootViewController:mainMenuVC];   [self.window addSubview:navigationController.view];
[self.window makeKeyAndVisible];
 return YES; 
[mainMenuVC release];
}
 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
 {
 
 return 1;

}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
 {
 
 return 3;

}

- (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];

    }
      
if((indexPath.row ) == 0)
  

[cell setText:@"Green"];
  
if((indexPath.row ) == 1)
  
[cell setText:@"Blue"];
 
  if((indexPath.row ) == 2)
 
 [cell setText:@"Magenta"];
  
return cell;

}


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


    if((indexPath.row ) == 0)
  
{
      
view1 = [[first alloc] init];
 
 [self.navigationController presentModalViewController:dvc1 animated:YES];

    [view1 release];

    }
  
if((indexPath.row)==1)
  
{
  
view2=[[second alloc]init];

    [self.navigationController presentModalViewController:dvc2 animated:YES];
  
[view2 release];
  
}
  
if((indexPath.row)==2)
  
{
      
view3=[[third alloc]init];
  
    [self.navigationController presentModalViewController:dvc3 animated:YES];
  
    [view3 release];
    }
  

}


- (void)dealloc
 {
    [navigationController release];
    [window release];
    [super dealloc];
}
@end








1 comment:

  1. This comment has been removed by a blog administrator.

    ReplyDelete