swift 4.2 - 根据字符串 push指定控制器
生活随笔
收集整理的這篇文章主要介紹了
swift 4.2 - 根据字符串 push指定控制器
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
倆個方法?
?
1. 創(chuàng)建類寫成 類方法
import UIKit /** 注釋:獲得VC* 1.字符串 和使用的控制器,直接跳轉(zhuǎn)* 2.用過字符串獲得對應(yīng)VC*/ class JYGetPushVc: NSObject {/// 指定字符串VC跳轉(zhuǎn),設(shè)置titlestatic func pushVcByVcNameAndTitle(pushVcNameStr:String, pushVcTitleStr:String? = nil, weakVc:UIViewController?){guard let namespace = Bundle.main.infoDictionary?["CFBundleExecutable"]as? String else{return debugPrint("JYGetPushVc 調(diào)用 pushVcByVcNameAndTitle, namespace不存在")}let clsName = namespace + "." + pushVcNameStrguard let cls = NSClassFromString(clsName) as? UIViewController.Type else{return debugPrint("JYGetPushVc 調(diào)用 pushVcByVcNameAndTitle, 項目中沒有控制器 === \(pushVcNameStr)")}let vc = cls.init()if let titleStr = pushVcTitleStr{vc.title = titleStr}weakVc?.navigationController?.pushViewController(vc, animated: true)}/// 根據(jù)字符串獲得對應(yīng)控制器,使用的時候as, 傳遞參數(shù)static func getVc(pushVcNameStr:String) -> UIViewController?{guard let namespace = Bundle.main.infoDictionary?["CFBundleExecutable"]as? String else{debugPrint("JYGetPushVc 調(diào)用 getVc, namespace不存在")return nil}let clsName = namespace + "." + pushVcNameStrguard let cls = NSClassFromString(clsName) as? UIViewController.Type else{debugPrint("JYGetPushVc調(diào)用getVc項目中沒有 控制器 === \(pushVcNameStr)")return nil}return cls.init()} }
類使用
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {//這里 VC需要傳遞參數(shù)進去的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}//這是主頁面看需求隱藏tabbarself.hidesBottomBarWhenPushed = trueif let vc = pushVc{self.navigationController?.pushViewController(vc, animated: true)}else{//這里不需要指定控制器。設(shè)置VC的屬性的。JYGetPushVc.pushVcByVcNameAndTitle(pushVcNameStr: vcNameArr[indexPath.section][indexPath.row], pushVcTitleStr: titleArr[indexPath.section][indexPath.row], weakVc: self)}//跳轉(zhuǎn)打開,不然回到首頁 沒有tabbarself.hidesBottomBarWhenPushed = false}
?
?
2. 在當前控制器 寫倆方法
方法1
/// 指定字符串VC跳轉(zhuǎn),設(shè)置titlefunc pushVcByVcNameAndTitle(vcName:String, vcTitleName:String = "", isHideBottomBar:Bool = false){if let namespace = Bundle.main.infoDictionary?["CFBundleExecutable"]as? String{let clsName = namespace + "." + vcNameif let cls = NSClassFromString(clsName) as? UIViewController.Type{let vc = cls.init()vc.title = vcTitleNameself.navigationController?.pushViewController(vc, animated: true)}}}
方法2
/// 根據(jù)字符串獲得對應(yīng)控制器,使用的時候as, 傳遞參數(shù)func pushVcByVcNameAndTitle(vcName:String) -> UIViewController?{if let namespace = Bundle.main.infoDictionary?["CFBundleExecutable"]as? String{let clsName = namespace + "." + vcNameif let cls = NSClassFromString(clsName) as? UIViewController.Type{let vc = cls.init()return vc}}return nil}
3.方法使用
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {//這里 VC需要傳遞參數(shù)進去的var pushVc : UIViewController?//具體VC 設(shè)置 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}//這是主頁面看需求隱藏tabbarself.hidesBottomBarWhenPushed = trueif let vc = pushVc{self.navigationController?.pushViewController(vc, animated: true)}else{//這里不需要指定控制器。設(shè)置VC的屬性的。pushVcByVcNameAndTitle(vcName: vcNameArr[indexPath.section][indexPath.row], vcTitleName: titleArr[indexPath.section][indexPath.row], isHideBottomBar: true)}//跳轉(zhuǎn)打開,不然回到首頁 沒有tabbarself.hidesBottomBarWhenPushed = false}
?
轉(zhuǎn)載于:https://www.cnblogs.com/qingzZ/p/10069503.html
總結(jié)
以上是生活随笔為你收集整理的swift 4.2 - 根据字符串 push指定控制器的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux(centOS7,mini),
- 下一篇: Python可视化库matplotlib