blkerai blkerai - 5 months ago 21
iOS Question

How to Activate Tab IOS

How to set selected tab in objective c,
i am adding tab programmatically,
Here is my code for that:

#import "TabControllerViewController.h"
#import "DetailsView.h"

@interface TabControllerViewController (){
NSMutableArray *kirtanDetail;
CGPoint startPosition;
}

@end

@implementation TabControllerViewController

- (void)viewDidLoad {
[super viewDidLoad];
dbm=[[DBManager alloc]init];
[dbm openDB];
kirtanDetail=[dbm getAllPad:[NSString stringWithFormat:@"%@",_kirtanID]];
// Do any additional setup after loading the view.
[self addTabView:[kirtanDetail count]];
[self.tabBarController setDelegate:self];
self.tabBarController.selectedIndex = 2;
}

-(void)viewDidAppear:(BOOL)animated{
// [[NSNotificationCenter defaultCenter] postNotificationName:@"activeTab" object:@"2"];
}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}


-(void)addTabView:(NSInteger *) tab {
UIStoryboard* sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
NSMutableArray *tabViewControllers = [[NSMutableArray alloc] init];

for(int i=0;i<tab;i++){
UIViewController *view1 = [sb instantiateViewControllerWithIdentifier: @"DV"];
[view1 viewDidLoad];
[tabViewControllers addObject:view1];
//can't set this until after its added to the tab bar
view1.tabBarItem =[[UITabBarItem alloc] initWithTitle:[NSString stringWithFormat:@"PAD %d",i+1] image:[UIImage imageNamed:@"view1"]tag:i];

UITextView *textView=[[view1.view subviews]objectAtIndex:0 ];
NSArray *data=[kirtanDetail objectAtIndex:i];
if([[data objectAtIndex:0] isEqualToString:[NSString stringWithFormat:@"%@",_kirtanID]]){
NSLog(@"Active TAB=%d",i);

}
textView.text=[NSString stringWithFormat:@"%@",[dbm getKirtanDetails:[NSString stringWithFormat:@"%@",[data objectAtIndex:0]]]];

}
[self setViewControllers:tabViewControllers];

}
-(void)setActiveTab:(NSNotification *)notification{
[self.tabBarController setSelectedIndex:[notification object]];
}

@end

Answer
[self.tabBarController setSelectedIndex:1];

Add this after viewDidLoad, in viewWillAppear

EDIT: Is this controller TabControllerViewController subclass of UITabBarController or UIVIewController? If it's subclass of UITabBarController then replace [self.tabBarController setSelectedIndex:1] with [self setSelectedindex:1]