2016-06-13 96 views
1

如何設置所選選項卡的目標C, 我加入標籤編程, 這裏是我的代碼:如何激活標籤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 

回答

1
[self.tabBarController setSelectedIndex:1]; 

添加此viewDidLoad中後,在viewWillAppear中

編輯: 這是控制器TabControllerViewController UITabBarController或UIVIewController的子類嗎?如果它是UITabBarController的子類,則用[self setSelectedindex:1]替換[self.tabBarController setSelectedIndex:1]

+0

我已嘗試但未能正常工作 – blkerai

+0

您什麼時候嘗試過?在哪個方法(viewDidLoad,viewWillAppear)? – tbilopavlovic

+0

我曾嘗試在這兩個方法 – blkerai