简体   繁体   English

在TabbarController中为特定索引设置ViewController

[英]Set ViewController for a specific Index in TabbarController

I have a UITabbarController with 5 ViewControllers as index number 0,1,2,3,4, Now, If its run in iPhone then 5 viewcontrollers loaded on it, that's fine, but if I changed iPhone to iPad, index number 4 should be different ViewControllers . 我有一个带有5个ViewControllersUITabbarController ,索引号为0、1、2、3、4,现在,如果它在iPhone中运行,则可以加载5个ViewController,这很好,但是如果我将iPhone更改为iPad,索引号应该为4不同的ViewControllers

Ex. 例如

if iPad{
    tabbarcontroller's index 4 should be B Viewcontroller
}else{
    tabbarcontroller's index 4 should be A Viewcontroller
}

I got an answer in objective c, link here but I can not make it out for Swift, Here is my code: 我在目标c中得到了一个答案,请在此处链接,但是我无法在Swift中找到答案,这是我的代码:

let settingVc = self.storyboard?.instantiateViewController(withIdentifier: "SettingTabbarController") as! SettingTabbarController

var controllers : NSMutableArray =  [self.viewControllers!][0] as! NSMutableArray

print(controllers)

controllers.replaceObject(at: 4, with: settingVc)

self.setViewControllers(controllers, animated: true)

Any help will be appreciated. 任何帮助将不胜感激。

Here you can change the viewController for specific index like below 在这里,您可以更改viewController的特定索引,如下所示

in your UITabbarController 在您的UITabbarController

   self.viewControllers?.remove(at: 4)
   let newItemController = UIStoryboard(name: "Settings", bundle: nil).instantiateViewController(withIdentifier: "SettingsViewController") as! SettingsViewController
   newItemController.title = "Settings"
   newItemController.tabBarItem.image = UIImage(named: "ic_tab_settings")
   newItemController.tabBarItem.isEnabled = true
   self.viewControllers?.insert(newItemController, at:4)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM