PDA

View Full Version : didSelectViewController not working. (call webservice when user clicks the Tab)




jjgraz
Apr 25, 2009, 03:01 PM
I have done a bit of research but can't get this thing to work. Perhaps if i post my code someone can point out the flaw.....I appreciate your help.

As you guessed.....I have A UITabBarController, it has 5 tabs. What im trying to do is: User selects TabBar Tab number 4 and it calls webservice to update my TableView. The call to server works fine,(it has been tested in ViewDidLoad), But I want it to call to server each time the user selects the tab.........Just can't figure out the Delegate for TabBar, it appears as though the didSelectViewController method is not working properly. See my code if you think you can help....

blt_applicationAppDelegate.h


#import <UIKit/UIKit.h>

@interface blt_applicationAppDelegate : NSObject <UIApplicationDelegate, UITabBarControllerDelegate> {
UIWindow *window;
IBOutlet UITabBarController *rootController;
}

@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) UITabBarController *rootController;


@end


blt_applicationAppDelegate.m


#import "blt_applicationAppDelegate.h"
#import "ViewController.h"

@implementation blt_applicationAppDelegate

@synthesize window;
@synthesize rootController;

- (void)applicationDidFinishLaunching:(UIApplication *)application {
[window addSubview:rootController.view];
[rootController setDelegate:self];
[window makeKeyAndVisible];
}

- (void)dealloc {
[rootController release];
[window release];
[super dealloc];
}

@end





ViewController.h



#import <UIKit/UIKit.h>

@class blt_applicationAppDelegate;

@interface ViewController : UITableViewController {
blt_applicationAppDelegate *appDelegate;
}

@end



ViewContoller.m


#import "ViewController.h"
#import "blt_applicationAppDelegate.h"

@implementation ViewController;

- (void)tabBarController:(UITabBarController *)rootController didSelectViewController:(UIViewController *)viewController {
if (1 == 1)
{
*********my code to call webservice*******
}
}

- (void)viewDidLoad {
[super viewDidLoad];
}

- (void)dealloc {
[appDelegate release];
[super dealloc];
}

@end



Thank you in advance for any help.



jjgraz
Apr 25, 2009, 06:16 PM
realized I declared that app delegate implements the protocol UITabBarControllerDelegate. Thus the method needs to be in appDelegate. However, any clues why following method doesn't seem to work quite right. In the XIB file I gave the viewcontrollers titles.....one of which is Item 5....why doesn't this work?


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

if ([rootController.tabBarItem.title isEqualToString:@"Item 5"])

{
********bunch of code which should be called if they select tabBarItem with title equal to Item 5******
[window addSubview:rootController.view];
[window addSubview:[navigationController view]];
}
else if ([rootController.tabBarItem.title isEqualToString:@"Item 4"])
{
}
else if ([rootController.tabBarItem.title isEqualToString:@"Item 3"])
{
}
else if ([rootController.tabBarItem.title isEqualToString:@"Item 2"])
{
}
}


Thank you.

jjgraz
Apr 25, 2009, 06:50 PM
Nevermind......I got it.

mixstah
Feb 4, 2011, 04:56 PM
Hi please could you post what you figured out I am trying to do something similar>