俩个方法

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
}

  

swift 4.2 - 根据字符串 push指定控制器的更多相关文章

  1. The Swift Programming Language-官方教程精译Swift(4)字符串和字符

    String 是一个有序的字符集合,例如 "hello, world", "albatross".Swift 字符串通过 String 类型来表示,也可以表示为 ...

  2. Swift语言指南(十)--字符串与字符

    原文:Swift语言指南(十)--字符串与字符 字符串是一段字符的有序集合,如"hellow,world"或"信天翁".Swift 中的字符串由 String ...

  3. pop回指定控制器

    //OCNSArray *array = [NSMutableArray new]; array = self.navigationController.viewControllers; //1.返回 ...

  4. Swift语言—有趣的字符串连接、数组、字典

    字符串链接:Swift语言中的字符串连接方式本人觉得非常的有趣,变量连接需要用右斜杠,并且变量名要括起来 “\(变量名)”,后面的字符串连接分别用逗号 ‘ , ’ 隔开 数组: Var arr = [ ...

  5. JavaScript trim 实现(去除字符串首尾指定字符)

    String.prototype.trim = function (char, type) { if (char) { if (type == 'left') { return this.replac ...

  6. 读懂Swift 2.0中字符串设计思路的改变

    Swift提供了一种高性能的,兼容Unicode编码的String实现作为标准库的一部分.在 Swift2中,String类型不再遵守CollectionType协议.在以前,String类型是字符的 ...

  7. iOS 生成随机字符串 从指定字符串随机产生n个长度的新字符串

    随机字符串 - 生成指定长度的字符串 -(NSString *)randomStringWithLength:(NSInteger)len { NSString *letters = @"a ...

  8. C#去掉字符串头尾指定字符

    private void button2_Click(object sender, EventArgs e)        {//去掉字符串头尾指定字符            string MyInf ...

  9. str_repeat() 函数把字符串重复指定的次数。

    str_repeat() 函数把字符串重复指定的次数. str_repeat(string,repeat) 参数 描述 string 必需.规定要重复的字符串. repeat 必需.规定字符串将被重复 ...

随机推荐

  1. Excel VBA 使用笔记

    开发工具选项卡(Developer Tab) File Tab -> Options -> Customize Ribbon -> Main Tabs -> Developer ...

  2. 使用navigator对象,输出当前浏览器的信息

    <script type="text/javascript"> with(document) { write("你的浏览器信息:<ol>" ...

  3. Android进阶AIDL - 2018年4月14日

    参考:慕课网 --- 最后三集.Android开发艺术探索 1.在AS中创建aidl文件后,要编译一下才会在gen下生成debug文件: 2.AIDL 不支持short类型,常用的数据类型: 3.AI ...

  4. 注解(Annotation)是什么?

  5. c#栈和队列习题

    3.1 比较线性表.栈和队列这三种数据结构的相同点和不同点. 栈(Stack)是限定只能在表的一端进行插入和删除操作的线性表.队列(Queue)是限定只能在表的一端进行插入和在另一端进行删除操作的线性 ...

  6. KJMusic完整音乐项目

    KJMusic完整音乐项目 KJMusic是一个完整音乐项目,这个项目从欢迎页面到首页以及音乐播放页面都做得非常不错.并且本音乐支持本地音乐,和音乐电台,支持切换上下首个.本项目还支持侧滑出现menu ...

  7. ERROR: dump failed because assets could not be loaded

    在命令行输入命令,想查看到app的包名,报错,如下: D:\测试\测试\android-sdk_r16-windows\android-sdk-windows\platform-tools>aa ...

  8. Java中Pattern类的quote方法将任何字符串(包括正则表达式)都转换成字符串常量,不具有任何匹配功能

    Java中Pattern类的quote方法将任何字符串(包括正则表达式)都转换成字符串常量,不具有任何匹配功能. 下面是个例子: import org.junit.Test; import java. ...

  9. mysql 和 Oracle 数据类型对照

    MySQL与Oracle两种数据库在工作中,都是用的比较多的数据库,由于MySQL与Oracle在数据类型上有部分差异,在我们迁移数据库时,会遇上一定的麻烦,下面介绍MySQL与Oracle数据库数据 ...

  10. Mat取行或列

    Mat dst; dst = FeatureValue.colRange(j,j+).clone(); 直接使用Mat类中成员函数,方法,colRange对应的是列,rowRange对应的是行,从第j ...