swift3 UITabBarController創(chuàng)建子控制器的封裝

一、封裝方法 創(chuàng)建子控制器

    //調(diào)用封裝方法
    addChildViewController(LXRHomeViewController(), title: "首頁(yè)", imageName: "tabbar_home")
    addChildViewController(LXRMessageViewController(), title: "消息", imageName: "tabbar_message_center")
    addChildViewController(LXRDiscoverViewController(), title: "發(fā)現(xiàn)", imageName: "tabbar_discover")
    addChildViewController(LXRProfileViewController(), title: "我", imageName: "tabbar_profile")

    // Swift支持方法的重改:方法名稱相同.但是參數(shù)類型和個(gè)數(shù)不同
    // private在當(dāng)前文件中可以訪問(wèn),其他文件不能訪問(wèn)
    // swift3.0 private建議修改為fileprivate
    private func addChildViewController(_ childVc: UIViewController, title : String, imageName : String) {
        //1.設(shè)置自控制器的屬性
        childVc.title = title
        childVc.tabBarItem.image = UIImage(named: imageName)
        childVc.tabBarItem.selectedImage = UIImage(named: imageName + "_highlighted")
        //2.包裝導(dǎo)航控制器
        let childNav = UINavigationController(rootViewController: childVc)
        //3.添加控制器
        addChildViewController(childNav)
    }

二、根據(jù)Json文件,通過(guò)字符串獲取數(shù)據(jù)

創(chuàng)建MainVCSettings.json的json文件,并導(dǎo)入項(xiàng)目中,文件內(nèi)容如下:

    [
     {
        "vcName": "HomeViewController",
        "title": "首頁(yè)",
        "imageName": "tabbar_home"
     },
     {
         "vcName": "MessageViewController",
         "title": "消息",
         "imageName": "tabbar_message_center"
     },
     {
         "vcName": "DiscoverViewController",
         "title": "廣場(chǎng)",
         "imageName": "tabbar_discover"
     },
     {
         "vcName": "ProfileViewController",
         "title": "我",
         "imageName": "tabbar_profile"
     }
     ]

獲取不到文件路徑請(qǐng)按照這篇文章配置下
http://www.itdecent.cn/p/db888332206a

    //1.獲取JSON文件路徑
    guard let jsonPath = Bundle.main.path(forResource: "MainVCSettings.json", ofType: nil) else {
        LXRLog(message: "沒(méi)有獲取到對(duì)應(yīng)的文件路徑")
        return
    }
    //2.讀取json文件中的內(nèi)容
    guard let jsonData = try? Data(contentsOf: URL(fileURLWithPath: jsonPath)) else {
        LXRLog(message: "沒(méi)有獲取到j(luò)son文件中數(shù)據(jù)")
        return
    }
    //3.將Data轉(zhuǎn)成數(shù)組
    guard  let anyObject = try? JSONSerialization.jsonObject(with: jsonData, options: .mutableContainers) else {
        return
    }

    guard let dictArray = anyObject as? [[String : AnyObject]] else{
        return
    }
    //4.遍歷字典,獲取對(duì)應(yīng)的信息
    for dict in dictArray{
        //4.1獲取控制器的對(duì)應(yīng)的字符串
        guard let VcName = dict["vcName"] as? String else {
            continue
        }
        //4.2獲取控制器顯示的title
        guard let title = dict["title"] as? String else {
            continue
        }
        //4.3獲取控制器顯示的圖標(biāo)名稱
        guard let imageName = dict["imageName"] as? String else {
            continue
        }
        //4.4添加自控制器
        addChildViewController(VcName, title: title, imageName: imageName)
    }

    /**************************************************/

    // Swift支持方法的重改:方法名稱相同.但是參數(shù)類型和個(gè)數(shù)不同
    // private在當(dāng)前文件中可以訪問(wèn),其他文件不能訪問(wèn)

    private func addChildViewController(_ childVcName: String, title : String, imageName : String) {
        
        //0.獲得命名空間
        guard let nameSpace = (Bundle.main.infoDictionary!["CFBundleExecutable"] as? String)  else {
            LXRLog(message: "沒(méi)有獲取命名空間")
            return
        }
        //1.根據(jù)字符創(chuàng)獲取對(duì)應(yīng)的Class
        guard let ChildVcClass = NSClassFromString(nameSpace + "." + childVcName)  else {
            LXRLog(message: "沒(méi)有獲取到字符創(chuàng)對(duì)應(yīng)的Class")
            return
        }
        // 2.將對(duì)應(yīng)的AnyObject轉(zhuǎn)成控制器的類型
        guard let childVcType = ChildVcClass as? UIViewController.Type else {
            LXRLog(message: "沒(méi)有獲取對(duì)應(yīng)控制器的類型")
            return
        }
        // 3.創(chuàng)建對(duì)應(yīng)的控制器對(duì)象
        let childVc = childVcType.init()
        
        //4.設(shè)置自控制器的屬性
        childVc.title = title
        childVc.tabBarItem.image = UIImage(named: imageName)
        childVc.tabBarItem.selectedImage = UIImage(named: imageName + "_highlighted")
        //5.包裝導(dǎo)航控制器
        let childNav = UINavigationController(rootViewController: childVc)
        //6.添加控制器
        addChildViewController(childNav)
    }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容