When "programmatically" creates a UINavigationController and a UITabBarController, how do I access their functions (for example, viewWillAppear?)

I create my Nav and TabBar in code when I run through: IN: myAppDelegate.m

- (void)applicationDidFinishLaunching:(UIApplication *)application { 
    // set up a local nav controller which we will reuse for each view controller
    UINavigationController *localNavigationController;

    // create tab bar controller and array to hold the view controllers
    tabBarController = [[UITabBarController alloc] init];
    NSMutableArray *localControllersArray = [[NSMutableArray alloc] initWithCapacity:4];

    // setup the first view controller (Root view controller)
    RootViewController *myViewController;
    myViewController = [[RootViewController alloc] initWithTabBar];

    // create the nav controller and add the root view controller as its first view
    localNavigationController = [[UINavigationController alloc] initWithRootViewController:myViewController];

    // add the new nav controller (with the root view controller inside it)
    // to the array of controllers
    [localControllersArray addObject:localNavigationController];

    // release since we are done with this for now
    [localNavigationController release];
    [myViewController release];

    // setup the first view controller just like the first 
    ResortsListViewController *resortsListViewController;
    resortsListViewController = [[ResortsListViewController alloc] initWithNibName:@"ResortsListView" bundle:nil];
    resortsListViewController.title = @"Category1";
    resortsListViewController.tabBarItem.image = [UIImage imageNamed:@"image1.png"];
    resortsListViewController.navigationItem.title=@"Category1";
    localNavigationController = [[UINavigationController alloc] initWithRootViewController:resortsListViewController];
    [localControllersArray addObject:localNavigationController]; 
    [localNavigationController release];

    // setup the second view controller just like the first 
    ResortsListViewController *resortsListViewController;
    resortsListViewController = [[ResortsListViewController alloc] initWithNibName:@"ResortsListView" bundle:nil];
    resortsListViewController.title = @"Category2";
    resortsListViewController.tabBarItem.image = [UIImage imageNamed:@"image2.png"];
    resortsListViewController.navigationItem.title=@"Category2";
    localNavigationController = [[UINavigationController alloc] initWithRootViewController:resortsListViewController];
    [localControllersArray addObject:localNavigationController]; 
    [localNavigationController release];

    // setup the third view controller just like the first 
    ResortsListViewController *resortsListViewController;
    resortsListViewController = [[ResortsListViewController alloc] initWithNibName:@"ResortsListView" bundle:nil];
    resortsListViewController.title = @"Category3";
    resortsListViewController.tabBarItem.image = [UIImage imageNamed:@"image3.png"];
    resortsListViewController.navigationItem.title=@"Category3";
    localNavigationController = [[UINavigationController alloc] initWithRootViewController:resortsListViewController];
    [localControllersArray addObject:localNavigationController]; 
    [localNavigationController release];

    [resortsListViewController release];

    // load up our tab bar controller with the view controllers
    tabBarController.viewControllers = localControllersArray;

    // release the array because the tab bar controller now has it
    [localControllersArray release];

    // add the tabBarController as a subview in the window
    [window addSubview:tabBarController.view];

    // need this last line to display the window (and tab bar controller)
    [window makeKeyAndVisible];


}

As you can see, I am reusing ResortsListViewController for various categories (resorts with beaches, resorts with pools, resorts with espresso bars) ... now, without bothering me (smiling) about the stupidity of my categories (cos is a test application) I need to do few things:

  • , ResortsListViewController. TAG, "initWithRootViewController" "tag". , , , ... navigationItem. , , ResortsListViewController , . "", , , tabbarcontroller.

  • , ResortsListViewController ..... TABLEVIEW, , . :

http://discussions.apple.com/thread.jspa?threadID=1529769&tstart=0

:

UINavigationControllers "ViewWill///Disappear" , "viewWill/Did/Appear/Disappear" .

UINavigationControllers ? myAppDelegate .h :

NSObject <UIApplicationDelegate, CLLocationManagerDelegate>

:

- (void)viewWillAppear:(BOOL)animated {
}

. , : "NSObject -viewWillAppear" .

?

+5
3

, : , , . , , :

ResortViewController int whichChoice (). TabBarController a la:

// setup the first view controller just like the first 
    ResortsListViewController *resortsListViewController;
    resortsListViewController = [[ResortsListViewController alloc]     initWithNibName:@"ResortsListView" bundle:nil];
    //  START HERE IS THE CHANGE
    resortsListViewController.whichChoice = 1;
    //  END HERE IS THE CHANGE
    resortsListViewController.title = @"Category1";
    resortsListViewController.tabBarItem.image = [UIImage imageNamed:@"image1.png"];
    resortsListViewController.navigationItem.title=@"Category1";
    localNavigationController = [[UINavigationController alloc] initWithRootViewController:resortsListViewController];
    [localControllersArray addObject:localNavigationController]; 
    [localNavigationController release];

, , resortsListViewController , : whichChoice, .

, . , , , , - , . , , "whichChoice" , ... KNOWN - .

# 2 IBOutlet, , IBOutlet . - VIEW "Referencing Outlet" [self.tableview reloadData] .

. , , -- IB, IB, . , ObjectId ( - ). . , IB , , gee wilikers... ! , IB ..., , . ... .

+1

1) . delegate UITabBarController. , tabbar ( , ). , :

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController

. , ( , , ).

2) UIViewController, (, ResortsListViewController), viewWillAppear :

@implementation ResortsListViewController

- (id)initWithNibName:(NSString *)name bundle:(NSBundle *)bundle {
...
}

... etc. ....

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

...

@end

, .

+2

Please visit How to Add a UINavigationController Programmatically

Please follow the link and get all the information about the UINavigationController.

-1
source

All Articles