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 必需.规定字符串将被重复 ...
随机推荐
- Leetcode 题解 Combinations:回溯+求排列组合
罗列出从n中取k个数的组合数组. 首先,求C(n,k)这个实现,很粗糙,溢出也不考虑,好的方法也不考虑.笨蛋.心乱,上来就写.. 另外,发现在递归中,不能申请太大的数组?貌似不是这个问题,是我自己越界 ...
- ReentrantLock 学习笔记
有篇写的很不错的博客:https://blog.csdn.net/aesop_wubo/article/details/7555956 基于JDK1.8 参考着看源码 ,弄清楚lock()和un ...
- Servlet基本_Httpリクエスト、レスポンス
1.リクエスト リクエストは.リクエストライン.メッセージヘッダ.改行.メッセージボディで組まれる. 主なリクエストヘッダは. Accept クライアントが利用可能なデータメディアタイプを指定. Ac ...
- (转)关闭win10的Skype
https://blog.csdn.net/qq_38285661/article/details/86663849 使用win10的小伙伴们,有没有发现一个不用的功能Skype,假如你想卸载又怕卸不 ...
- ubuntu 下 rvm 卸载和重装
卸载: sudo apt-get --purge remove ruby-rvm sudo rm -rf /usr/share/ruby-rvm /etc/rvmrc /etc/profile.d/r ...
- C# 监测每个方法的执行次数和占用时间(测试1)
在Nuget引用 Castle.DynamicProxy 和 Newtonsoft.Json 这个 原文:http://www.cnblogs.com/RicCC/archive/2010/03/15 ...
- Oracle 导入大量数据
环境是这样的: 需要导入大量数据到Oracle,目前Oracle已建立索引和触发器了,导入的数据是树型结构,需要关联. 采用的方法是: 删除以前数据库的索引和触发器,用OracleBulkCopy批量 ...
- 案例:Spark基于用户的协同过滤算法
https://mp.weixin.qq.com/s?__biz=MzA3MDY0NTMxOQ==&mid=2247484291&idx=1&sn=4599b4e31c2190 ...
- destoon手机端分页
$pages = ''; $pagesize = 2; $offset = ($page-1)*$pagesize; $rr = $db->get_one("SELECT COUNT( ...
- react input的几个坑
[react input的几个坑] 1.input标签中设置value后,input进入controlled模式,valuechange由自动变为手动,导致input无法编辑.如: <input ...