I have added Two Tabs in my app, to load two view controller using these tabs
- Tab1 : Home
- Tab2 : Favourite
so I have written below code to achieve this
In app Delegate
AppDelegate.h
@class ViewController,FavViewController;
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) ViewController *viewController;
@property (strong, nonatomic) FavViewController *favViewController;
@property (strong, nonatomic) UITabBarController *tabBarController;
@end
AppDelegate.m
@implementation AppDelegate
@synthesize window = _window;
@synthesize viewController;
@synthesize favViewController;
@synthesize tabBarController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
baseTabBarController = [[UITabBarController alloc]init];
baseTabBarController.delegate=self;
viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
UINavigationController *homeView = [[UINavigationController alloc]initWithRootViewController:viewController];
favViewController = [[FavViewController alloc] initWithNibName:@"FavViewController" bundle:nil];
favViewController.title = @"My Favourite";
UINavigationController *favouriteView = [[UINavigationController alloc] initWithRootViewController:favViewController];
NSArray *controllers = [NSArray arrayWithObjects:homeView,favouriteView, nil];
baseTabBarController.viewControllers = controllers;
[self.window addSubview:baseTabBarController.view];
[self.window makeKeyAndVisible];
return YES;
}
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)selectedViewController
{
if (tabBarController.selectedIndex == 0) {
}else if (tabBarController.selectedIndex == 1) {
[(FavViewController *)[(UINavigationController*)selectedViewController topViewController] getData];
}else if (tabBarController.selectedIndex == 2) {
NSLog(@"2");
}
}
and here is the Result Screen I am getting
So Where I am having trouble in this..
If I switch to the next screen my First tab doesn't load First Screen (Home screen) instead just stay on current screen.
Let me try with example
There are four screens in my app let say A, B, C, D
I have added Tabs for A and C screens,those are available in whole app (All screen).
Now if I'll start app and go A ->B->C -> D and press on Home Tab (A) it doesn't load Screen A, instead just stay on Current screen
but good thing is if i do same process with another tab C (My Favourite) it loads correct screen.
Edit: I have implemented didSelectViewController method as @sanjit suggested me but in that I not able distinguish, which tab is tapped this time?
I would be really appreciate!! if someone can point me on right direction