你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> iOS導航控制器和標簽控制器互相切換

iOS導航控制器和標簽控制器互相切換

編輯:IOS開發綜合

我的app應用登錄後,先是在一個navigation控制器裡添加tabelview,需要點擊cell,跳轉到tabBar控制器,在tabbar裡點擊navigation的leftBarButtonItem,又會把先前的tableview push出來,再點擊cell,又需要跳轉到tabBar,如此反復。

先前因為在這個跳轉過程中,沒有把流程屢清楚,導致這兩個東西在頻繁切換後會多次重復創建,消耗很多資源。最後閃退。

後來想做成單例,處理不好,又做成懶加載,懶加載屬性弄不好,又弄成應用代理的屬性懶加載,還是不行。

後來換個思路:因為我的tableview在tabbar裡是通過push它的父控制器導航控制器的,所以就用個全局bool變量記錄cell點擊次數,第一次點擊cell,切換根控制器到tabbar,以後再點擊cell就調用父控制器的pop,彈回來。壓棧--》出棧。

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
   /* + (instancetype)shareManager {
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            _shareInstanse = [[HMHTTPManager alloc] init];
        });
        return _shareInstanse;
    }*/
  /*  static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
     AppDelegate *appDelegate=(AppDelegate *)[[UIApplication sharedApplication] delegate];
    
    [UIApplication sharedApplication].keyWindow.rootViewController=[appDelegate manager];
    });*/
    self.longPressRow = indexPath.row;
    HighFrequencyList *item=[allNews objectAtIndex:self.longPressRow];
    currentName = item.names;
    currentIPAddress=item.ips;
    currentPort = item.portNumber;
    currentdeviceID=item.deviceID;
  
    if (firstTapOrNot == YES) {
        RunState1ViewController *runState=[[RunState1ViewController alloc]init];
        UINavigationController *nav1=[[XMGNavigationController alloc]initWithRootViewController:runState];
        nav1.tabBarItem.title=@"運行";
        nav1.tabBarItem.image=[UIImage imageNamed:@"運行圖標"];
        nav1.tabBarItem.selectedImage=[UIImage imageNamed:@"運行圖標--點擊時態"];
        // nav1.tabBarItem.badgeValue=@"32";
        settingViewController *setting=[[settingViewController alloc]init];
        UINavigationController *nav2=[[XMGNavigationController alloc]initWithRootViewController:setting];
        nav2.tabBarItem.title=@"參數";
        nav2.tabBarItem.image=[UIImage imageNamed:@"338280223"];
        nav2.tabBarItem.selectedImage=[UIImage imageNamed:@"參數-點擊時態"];
        
        GMLRunStopViewController *runStop=[[GMLRunStopViewController alloc]init];
        UINavigationController *nav3=[[XMGNavigationController alloc]initWithRootViewController:runStop];
        
        nav3.tabBarItem.title=@"啟停";
        nav3.tabBarItem.image=[UIImage imageNamed:@"啟停-常態"];
        nav3.tabBarItem.selectedImage=[UIImage imageNamed:@"啟停--點擊時態"];
        
        warningViewController *warning=[[warningViewController alloc]init];
        UINavigationController *nav4=[[XMGNavigationController alloc]initWithRootViewController:warning];
        nav4.tabBarItem.title=@"告警";
        nav4.tabBarItem.image=[UIImage imageNamed:@"告警-常態"];
        nav4.tabBarItem.selectedImage=[UIImage imageNamed:@"告警點擊時態"];
        XMGTabBarController *tabBar=[[XMGTabBarController alloc]init];
        [tabBar addChildViewController:nav1];
        [tabBar addChildViewController:nav2];
        [tabBar addChildViewController:nav3];
        [tabBar addChildViewController:nav4];
    
        [UIApplication sharedApplication].keyWindow.rootViewController=tabBar;
        firstTapOrNot = NO;
 
    } else {
        [self.navigationController popViewControllerAnimated:YES];

    }
    
    
//[appDelegate manager];//self.manager;//;
   //tabBarVc.delegate = (id)[UIApplication sharedApplication].delegate;
/*    NSString *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject;
    NSString *fileName = [path stringByAppendingPathComponent:@"connectConfigure.plist"];
    NSArray *result=[NSArray arrayWithContentsOfFile:fileName];
    IPAddress = result[0];if(IPAddress == nil) IPAddress=@"192.168.3.153";
    name = result[1];if(name == nil) name=@"1A1";
    port= result[2];if(port ==nil) port=@"502";
    deviceID = result[3];if(deviceID == nil) deviceID=@"1";
    */

   // GMLIPAddress = result[0];
 //NSLog(@"%@--%@--%d--%d",currentIPAddress,currentName,currentPort,currentdeviceID);
   // NSLog(@"%d",self.longPressRow);
  /*
    NSArray *mingzi=[NSArray arrayWithObjects:@"2A11",@"2A12",@"2A13",
                      @"2A21",@"2A22",@"2A23",
                      @"2B11",@"2B12",@"2B13",
                      @"2B21",@"2B22",@"2B23",nil];
    NSArray *IPdizhi = [NSArray arrayWithObjects:@"192.168.0.11",@"192.168.0.12",@"192.168.0.13",
                        @"192.168.0.21",@"192.168.0.22",@"192.168.0.23",
                        @"192.168.0.31",@"192.168.0.32",@"192.168.0.33",
                        @"192.168.0.41",@"192.168.0.42",@"192.168.0.43", nil];
    if (indexPath.row <= 11 ) {
        IPAddress = IPdizhi[indexPath.row];
        name = mingzi[indexPath.row];
        port= @"502";
        deviceID = @"1";
       
    }*/
    /*
    NSLog(@"當前ip地址是%@",IPAddress);
    NSLog(@"當前端口號是%@",port);
    NSLog(@"當前設備號是%@",deviceID);
    NSLog(@"當前名字是%@",name);
    */
    //tabBar.delegate = (id)[UIApplication sharedApplication].delegate;
   /* UIStoryboard *storyboard = [UIStoryboard storyboardWithName:NSStringFromClass([RunState1ViewController class]) bundle:nil];
    // 加載箭頭指向控制器
    RunState1ViewController *meVc =[storyboard instantiateViewControllerWithIdentifier:@"runState1"];
    [self presentViewController:meVc animated:NO completion:nil];*/
}

還有,要把一個東西弄成懶加載,需要把這個東西弄成這個類的屬性,寫一個get方法,如果沒有就創建,創建好了賦給屬性,如果有就直接返回。

我看別人寫的大多是視圖做成懶加載,創建出來後,多半要[xxx addSubview:yyy];我以為控制器也需要這樣弄,發現不知道add到哪個上面,弄錯了,程序崩潰,說找不到父視圖,控制器實際上根本不用。

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved