2016-06-13 12 views
1

Wie stellt ausgewählte Registerkarte in Objective-C zu aktivieren, i Registerkarte bin Hinzufügen programmatisch ist Hier mein Code dafür:Wie Tab IOS

#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 

Antwort

1
[self.tabBarController setSelectedIndex:1]; 

Fügen Sie diese nach viewDidLoad, in viewWillAppear

EDIT: Ist dieser Controller TabControllerViewController Unterklasse von UITabBarController oder UIVIewController? Wenn es eine Unterklasse von UITabBarController ist, dann ersetze [self.tabBarController setSelectedIndex:1] durch [self setSelectedindex:1]

+0

ich hatte es versucht, aber nicht funktioniert – blkerai

+0

Wann haben Sie es versucht? In welcher Methode (viewDidLoad, viewWillAppear)? – tbilopavlovic

+0

Ich hatte in beiden Methoden versuchen – blkerai

Verwandte Themen