如何在目标c中设置选定的标签, 我正在以编程方式添加标签, 这是我的代码:
#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
答案 0 :(得分:1)
[self.tabBarController setSelectedIndex:1];
在viewDillLoad之后,在viewWillAppear
中添加此内容编辑:
这是UITabBarController或UIVIewController的控制器TabControllerViewController子类吗?如果它是UITabBarController的子类,则将[self.tabBarController setSelectedIndex:1]
替换为[self setSelectedindex:1]