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
}
swift 4.2 - 根据字符串 push指定控制器的更多相关文章
- The Swift Programming Language-官方教程精译Swift(4)字符串和字符
String 是一个有序的字符集合,例如 "hello, world", "albatross".Swift 字符串通过 String 类型来表示,也可以表示为 ...
- Swift语言指南(十)--字符串与字符
原文:Swift语言指南(十)--字符串与字符 字符串是一段字符的有序集合,如"hellow,world"或"信天翁".Swift 中的字符串由 String ...
- pop回指定控制器
//OCNSArray *array = [NSMutableArray new]; array = self.navigationController.viewControllers; //1.返回 ...
- Swift语言—有趣的字符串连接、数组、字典
字符串链接:Swift语言中的字符串连接方式本人觉得非常的有趣,变量连接需要用右斜杠,并且变量名要括起来 “\(变量名)”,后面的字符串连接分别用逗号 ‘ , ’ 隔开 数组: Var arr = [ ...
- JavaScript trim 实现(去除字符串首尾指定字符)
String.prototype.trim = function (char, type) { if (char) { if (type == 'left') { return this.replac ...
- 读懂Swift 2.0中字符串设计思路的改变
Swift提供了一种高性能的,兼容Unicode编码的String实现作为标准库的一部分.在 Swift2中,String类型不再遵守CollectionType协议.在以前,String类型是字符的 ...
- iOS 生成随机字符串 从指定字符串随机产生n个长度的新字符串
随机字符串 - 生成指定长度的字符串 -(NSString *)randomStringWithLength:(NSInteger)len { NSString *letters = @"a ...
- C#去掉字符串头尾指定字符
private void button2_Click(object sender, EventArgs e) {//去掉字符串头尾指定字符 string MyInf ...
- str_repeat() 函数把字符串重复指定的次数。
str_repeat() 函数把字符串重复指定的次数. str_repeat(string,repeat) 参数 描述 string 必需.规定要重复的字符串. repeat 必需.规定字符串将被重复 ...
随机推荐
- springmvc配置接口返回的数据是json
首先要导入所需要的jar,使用maven方式管理jar包 <!-- 配置接口返回数据json --> <dependency> <groupId>com.faste ...
- RN Component生命周期函数
https://www.race604.com/react-native-component-lifecycle/ 第一次加载时: getInitialProps getInitialState co ...
- 21.struts-Action配置.md
目录 1.Action开发方式 2.通配符 访问地址 [toc] 3.常量 后缀 指定默认编码集,作用于HttpServletRequest的setCharacterEncoding方法和freema ...
- ShaderLab学习总结
转载 Unity ShaderLab学习总结 Why Bothers? 为什么已经有ShaderForge这种可视化Shader编辑器.为什么Asset Store已经有那么多炫酷的Shader组件可 ...
- android listview中item通过viewpager实现《IT蓝豹》
android listview中item通过viewpager实现 android listview中item通过viewpager实现,每一个item都支持viewpager实现图片切换功能.本项 ...
- Oracle 查询版本号
select * from v$version; -- 或 select banner from sys.v_$version;
- javascript学习笔记(三):运算符、循环语句
javascript的运算符.条件语句.循环语句的使用方法大部分和c语言类似,但是值得注意的是,运算符中"=="和"==="的使用方法和c语言有区别:在java ...
- Unable to compile class for JSP
https://www.cnblogs.com/mthoutai/p/7136304.html 错误提示: The return type is incompatible with JspSource ...
- tomcat 启动报错org.hibernate.cfg.annotations.SimpleValueBinder.setType
url: https://blog.csdn.net/zhx_0323/article/details/78844323 # A fatal error has been detected by th ...
- VM虚拟机 Centos7 lnmp环境 配置域名问题 windows浏览器访问的问题
CentOS7 ip地址为 192.168.0.155 搭有 LNMP集成环境 执行 lnmp vhost add 配置服务器 指定目录的域名 mark.com 这时windows 机器的 浏览器想 ...