俩个方法
1. 创建类写成 类方法
import UIKit
/** 注释:获得VC* 1.字符串 和使用的控制器,直接跳转* 2.用过字符串获得对应VC*/
class JYGetPushVc: NSObject {/// 指定字符串VC跳转,设置titlestatic 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 + "." + pushVcNameStrguard 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 + "." + pushVcNameStrguard 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}//这是主页面看需求隐藏tabbarself.hidesBottomBarWhenPushed = trueif 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)}//跳转打开,不然回到首页 没有tabbarself.hidesBottomBarWhenPushed = false}
2. 在当前控制器 写俩方法
方法1
/// 指定字符串VC跳转,设置titlefunc pushVcByVcNameAndTitle(vcName:String, vcTitleName:String = "", isHideBottomBar:Bool = false){if let namespace = Bundle.main.infoDictionary?["CFBundleExecutable"]as? String{let clsName = namespace + "." + vcNameif let cls = NSClassFromString(clsName) as? UIViewController.Type{let vc = cls.init()vc.title = vcTitleNameself.navigationController?.pushViewController(vc, animated: true)}}}
方法2
/// 根据字符串获得对应控制器,使用的时候as, 传递参数func pushVcByVcNameAndTitle(vcName:String) -> UIViewController?{if let namespace = Bundle.main.infoDictionary?["CFBundleExecutable"]as? String{let clsName = namespace + "." + vcNameif 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}//这是主页面看需求隐藏tabbarself.hidesBottomBarWhenPushed = trueif 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)}//跳转打开,不然回到首页 没有tabbarself.hidesBottomBarWhenPushed = false}