swift 4.2 - 根据字符串 push指定控制器

俩个方法 

 

1. 创建类写成 类方法

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()
    }
}

  

类使用

    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

 /// 指定字符串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

    /// 根据字符串获得对应控制器,使用的时候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.方法使用

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

        //这里 VC需要传递参数进去的
        var pushVc : UIViewController?

//具体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 }

  

 

转载于:https://www.cnblogs.com/qingzZ/p/10069503.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值