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 必需.规定字符串将被重复 ...
随机推荐
- python tuple排序
tuple排序,按照索引0,1,2,3...依次比较 a = [3,1,0,9,6,2,4,8,7,5] a.sort(key = lambda x: (x%2, x, x%3)) # 先按照x%2的 ...
- 剑指OFFER例题——从尾到头打印链表
/** * public class ListNode { * int val; * ListNode next = null; * * ListNode(int val) { * this.val ...
- 【388】※ Some useful websites for learning Python
Ref: Python Tips 1. *args and **kwargs 2. Debugging 3. Generators 4. Map, Filter and Reduce 5. set D ...
- [Sphinx]全文索引Sphinx的使用配置
-------------------------------------------------------------------------------------- 搜索分为两种: 1. 对结 ...
- nth-child与nth-of-type区别
示例详细理解:nth-child(n)与:nth-of-type(n)区别 childselector:nth-child(index) 1,子选择器(childselector,这里是p选择器)选中 ...
- 吴裕雄 oracle PL/SQL编程
- hbase高可用集群部署(cdh)
一.概要 本文记录hbase高可用集群部署过程,在部署hbase之前需要事先部署好hadoop集群,因为hbase的数据需要存放在hdfs上,hadoop集群的部署后续会有一篇文章记录,本文假设had ...
- java 代码块,静态代码块,构造器等的执行顺序
写了一段测试代码,如下: public class ExecutionSequence extends fatherClass{ static{ System.out.printl ...
- HTTPConnectionPool(host:XX)Max retries exceeded with url
爬虫多次访问同一个网站一段时间后会出现错误 HTTPConnectionPool(host:XX)Max retries exceeded with url '<requests.package ...
- kafka 清除topic数据脚本
原 kafka 清除topic数据脚本 2018年07月25日 16:57:13 pete1223 阅读数:1028 #!/bin/sh param=$1 echo " ...