欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

iOS导航控制器和标签控制器互相切换

程序员文章站 2022-04-22 10:14:32
我的app应用登录后,先是在一个navigation控制器里添加tabelview,需要点击cell,跳转到tabBar控制器,在tabbar里点击navigation的leftB...

我的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到哪个上面,弄错了,程序崩溃,说找不到父视图,控制器实际上根本不用。