俩个方法

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. 04_JSX练习

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  2. image 标签src

    最近对接到前端  src需要填写  src= "data:image/jpg;base64,xxxxxxxooooooo";  记录一下图片转换的问题,需要把图片转换成base64 ...

  3. JS 打印图片

    在使用window.print()进行打印时,打印的内容可能会包含图片内容,此时的图片内容不能设置为背景图片,否则将无法再打印页面显示. <!doctype html> <html& ...

  4. javascript单线程那些事

    首先,说下为什么 JavaScript 是单线程? 总所周知,JavaScript是以单线程的方式运行的.说到线程就自然联想到进程.那它们有什么联系呢? 进程和线程都是操作系统的概念.进程是应用程序的 ...

  5. week5 0.2 client

    我们修改了下logo 自己找的图片 放在public文件下 页面如下我们准备做成这样 每一个component对应一个css样式 不需要统一的css 这样容易找到自己的css并修改 下面我们修改我们的 ...

  6. ArcGIS案例学习笔记-中国2000坐标转换实例

    ArcGIS案例学习笔记-中国2000坐标转换实例 联系方式:谢老师,135-4855-4328,xiexiaokui#qq.com 目的:西安1980.中国2000.WGS84(GPS)等任意坐标系 ...

  7. eclipse 连接数据库记录

    两篇很好的文章介绍给大家: Eclipse使用JDBC方式连接SQLServer2016 通过Driver与DriverManager连接数据库 --------------------------- ...

  8. windows 允许空密码登陆

    HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Lsa  这个注册表键值下的limitblankpassworduse项 修改为0或者1

  9. 搭建GitLab+Jenkins

    1. Jenkins and GitLab Jenkins是一个自动化服务器,可以运行各种自动化构建.测试或部署任务. GitLab是一个代码仓库,用来管理代码. 两者结合起来,就可以实现开发者提交代 ...

  10. CSS 美化radio checkbox

    CSS 样式 <style type="text/css"> .option-input { -webkit-appearance: none; -moz-appear ...