Reputation: 35783
I have added Two Tabs in my app, to load two view controller using these tabs
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
Upvotes: 1
Views: 5658
Reputation: 394
Viewcontroller.h
@property(nonatomic, retain) UITabBarItem *item1;
@property(nonatomic, retain) UITabBarItem *item2;
@property(nonatomic, retain) UITabBarItem *item3;
@property(nonatomic, retain) UITabBarItem *item4;
@property(nonatomic, retain) IBOutlet UITabBar *tabBar;
Viewcontroller.m
@synthesize item1,item2,item3,item4,tabBar;
- (void)viewDidLoad
{
item1 = [[UITabBarItem alloc] initWithTitle:@"Home" image:[UIImage imageNamed:@"home.png"] tag:0];
item2 = [[UITabBarItem alloc] initWithTitle:@"My Lists" image:[UIImage imageNamed:@"mylist"] tag:1];
item3 = [[UITabBarItem alloc] initWithTitle:@"Search" image:[UIImage imageNamed:@"search.png"] tag:2];
item4 = [[UITabBarItem alloc] initWithTitle:@"Settings" image:[UIImage imageNamed:@"setting.png"] tag:3];
NSArray *items = [NSArray arrayWithObjects:item1,item2,item3,item4, nil];
[tabBar setItems:items animated:NO];
[tabBar setSelectedItem:[tabBar.items objectAtIndex:2]];
tabBar.delegate=self;
[self.view addSubview:tabBar
];
}
/****Function Call******/
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
if(item.tag == 0)
{
Newsfeeds *obj = [[Newsfeeds alloc]initWithNibName:@"Newsfeeds" bundle:Nil];
[self.navigationController pushViewController:obj animated:NO];
}
else if(item.tag == 1)
{
MyLists *obj = [[MyLists alloc]initWithNibName:@"MyLists" bundle:Nil];
[self.navigationController pushViewController:obj animated:NO];
}
else if(item.tag == 3)
{
Settings *obj = [[Settings alloc]initWithNibName:@"Settings" bundle:Nil];
[self.navigationController pushViewController:obj animated:NO];
}
}
Upvotes: 0
Reputation: 341
replace the code
[viewController setTabBarItem:tab1];
[favViewController setTabBarItem:tab2];
with
[[tabbarController tabBar] setItems:[NSArray arrayWithObjects:tab1,tab2, nil]];
that should resolve.
Upvotes: 0
Reputation: 935
Use Tabbar delegate and using parameter instance of uiviewcontroller call poptorootviewcontroller method. Hope it may work for you. Try the following code
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
UINavigationController *navController = (UINavigationController*)tabBarController.selectedViewController;
[navController popToRootViewControllerAnimated:YES];
}
Upvotes: 3