随笔 - 400,  文章 - 0,  评论 - 7,  阅读 - 21万

俩个方法 

 

1. 创建类写成 类方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import UIKit
/*
 * 注释:获得VC
 * 1.字符串 和使用的控制器,直接跳转
 * 2.用过字符串获得对应VC
 */
class JYGetPushVc: NSObject {
 
    /// 指定字符串VC跳转,设置title
    static func pushVcByVcNameAndTitle(pushVcNameStr:String, pushVcTitleStr:String? = nil, weakVc:UIViewController?){
        guard let namespace = Bundle.main.infoDictionary?["CFBundleExecutable"]as? String else{
            return debugPrint("JYGetPushVc 调用 pushVcByVcNameAndTitle, namespace不存在")
        }
        let clsName = namespace + "." + pushVcNameStr
        guard let cls = NSClassFromString(clsName) as? UIViewController.Type else{
            return debugPrint("JYGetPushVc 调用 pushVcByVcNameAndTitle, 项目中没有控制器 === \(pushVcNameStr)")
        }
        let vc = cls.init()
        if let titleStr = pushVcTitleStr{
            vc.title = titleStr
        }
        weakVc?.navigationController?.pushViewController(vc, animated: true)
    }
     
    /// 根据字符串获得对应控制器,使用的时候as, 传递参数
    static func getVc(pushVcNameStr:String) -> UIViewController?{
         
        guard let namespace = Bundle.main.infoDictionary?["CFBundleExecutable"]as? String else{
            debugPrint("JYGetPushVc 调用 getVc, namespace不存在")
            return nil
        }
        let clsName = namespace + "." + pushVcNameStr
        guard let cls = NSClassFromString(clsName) as? UIViewController.Type else{
            debugPrint("JYGetPushVc调用getVc项目中没有 控制器 === \(pushVcNameStr)")
            return nil
        }
        return cls.init()
    }
}

  

类使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
 
    //这里 VC需要传递参数进去的
    var pushVc : UIViewController?
 
    if let vc = JYGetPushVc.getVc(pushVcNameStr: vcNameArr[indexPath.section][indexPath.row]) as? JYWorksShowController{
        vc.title = titleArr[indexPath.section][indexPath.row]
        pushVc = vc
    }
     
    //这是主页面看需求隐藏tabbar
    self.hidesBottomBarWhenPushed = true
     
    if let vc = pushVc{
        self.navigationController?.pushViewController(vc, animated: true)
    }else{
        //这里不需要指定控制器。设置VC的属性的。
        JYGetPushVc.pushVcByVcNameAndTitle(pushVcNameStr: vcNameArr[indexPath.section][indexPath.row], pushVcTitleStr: titleArr[indexPath.section][indexPath.row], weakVc: self)
    }
     
    //跳转打开,不然回到首页 没有tabbar
    self.hidesBottomBarWhenPushed = false
}

  

 

 

2. 在当前控制器 写俩方法

方法1

1
2
3
4
5
6
7
8
9
10
11
/// 指定字符串VC跳转,设置title
   func pushVcByVcNameAndTitle(vcName:String, vcTitleName:String = "", isHideBottomBar:Bool = false){
       if let namespace = Bundle.main.infoDictionary?["CFBundleExecutable"]as? String{
           let clsName = namespace + "." + vcName
           if let cls = NSClassFromString(clsName) as? UIViewController.Type{
               let vc = cls.init()
               vc.title = vcTitleName
               self.navigationController?.pushViewController(vc, animated: true)
           }
       }
   }

  

方法2

1
2
3
4
5
6
7
8
9
10
11
/// 根据字符串获得对应控制器,使用的时候as, 传递参数
func pushVcByVcNameAndTitle(vcName:String) -> UIViewController?{
    if let namespace = Bundle.main.infoDictionary?["CFBundleExecutable"]as? String{
        let clsName = namespace + "." + vcName
        if let cls = NSClassFromString(clsName) as? UIViewController.Type{
            let vc = cls.init()
            return vc
        }
    }
    return nil
}

  

3.方法使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
 
        //这里 VC需要传递参数进去的
        var pushVc : UIViewController?<br><br>       //具体VC 设置 vc的属性
        if let vc1 = pushVcByVcNameAndTitle(vcName: vcNameArr[indexPath.section][indexPath.row]) as? JYWorksShowController{
            vc1.title = titleArr[indexPath.section][indexPath.row]
            //vc1.arr = self.dataArr
            //vc1.title = vcTitleArr[index.row]
            pushVc = vc1
        }
 
        //这是主页面看需求隐藏tabbar
        self.hidesBottomBarWhenPushed = true
        if let vc = pushVc{
            self.navigationController?.pushViewController(vc, animated: true)
        }else{
            //这里不需要指定控制器。设置VC的属性的。
            pushVcByVcNameAndTitle(vcName: vcNameArr[indexPath.section][indexPath.row], vcTitleName: titleArr[indexPath.section][indexPath.row], isHideBottomBar: true)
        }
        //跳转打开,不然回到首页 没有tabbar
        self.hidesBottomBarWhenPushed = false
    }

  

 

posted on   懂事长qingzZ  阅读(508)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示