[英]Tabbar item in view based application not working
我已經在基於視圖的應用程序中通過筆尖創建了一個包含三個項目的標簽欄。
我希望在視圖出現時默認選擇第一項。
問題是選擇了item1 show,但是它不會加載它有權執行的視圖。 當我們單擊該項目時,將顯示視圖。 請幫我解決這個問題。 這是我的代碼...
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
tabBar.delegate = self;
[tabBar setSelectedItem:[tabBar.items objectAtIndex:0]];
}
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
NSLog(@"didSelectItem: %d", item.tag);
if (item.tag==1) {
ImagesOverlay=[[UIView alloc]initWithFrame:CGRectMake(0, 210, 320, 250)];
ImagesOverlay.backgroundColor=[UIColor grayColor];
[self.view addSubview:ImagesOverlay];
}else if (item.tag==2) {
relatedOverlay=[[UIView alloc]initWithFrame:CGRectMake(0, 210, 320, 250)];
relatedOverlay.backgroundColor=[UIColor redColor];
[self.view addSubview:relatedOverlay];
}else if(item.tag==3){
//other condition
}
}
剛完成。
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
tabBar.delegate = self;
[tabBar setSelectedItem:[tabBar.items objectAtIndex:0]];
[self activateTab:1];
}
- (void)activateTab:(int)index {
switch (index) {
case 1:
//condition
break;
case 2:
//condition
break;
default:
break;
}
}
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
NSLog(@"didSelectItem: %d", item.tag);
[self activateTab:item.tag];
}
看來您需要對UITabBarController
工作方式進行更多研究。 您應該向其傳遞UIViewController
實例,而不是手動更改視圖。 閱讀課程參考:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.