首页 > 编程语言 > 详细

swift 4.2 - 根据字符串 push指定控制器

时间:2018-12-05 10:57:37      阅读:150      评论:0      收藏:0      [点我收藏+]

俩个方法

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.使用 : 我这里是tableview使用的

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指定控制器

原文:https://www.cnblogs.com/qingzZ/p/10069503.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!