Swift 4 循环动态创建对象

通过NSClassFromString动态创建TabBar控制器的子控制器,主要注意点在获取其类名时还需要在之前加上工程名。

    //控制器的名字
    let ctrlNames = [
        "MainPageViewController",
        "NearShopViewController",
        "ChooseViewController",
        "MyViewController"]
    
    //标题
    let titles = ["首页","附近","精选","我的"]
    
    //tabbar图片
    let normalImageNames = [
    "icon_tab_shouye_normal",
    "icon_tab_fujin_normal",
    "icon_tab_selection_normal",
    "icon_tab_wode_normal"];
    
    //选中的tabbar图片
    let selectedImageNames = [
    "icon_tab_shouye_highlight",
    "icon_tab_fujin_highlight",
    "icon_tab_selection_highlight",
    "icon_tab_wode_highlight"];
    
    
    //动态创建对象
    let projectName = Bundle.main.infoDictionary?["CFBundleName"] as! String
    for i in 0..

你可能感兴趣的:(Swift 4 循环动态创建对象)