【问题标题】:Set ViewController for a specific Index in TabbarController为 TabbarController 中的特定索引设置 ViewController
【发布时间】:2017-07-06 12:42:12
【问题描述】:

我有一个UITabbarController,其中 5 个 ViewControllers 作为索引号 0、1、2、3、4,现在,如果它在 iPhone 中运行,然后加载 5 个视图控制器,那很好,但如果我将 iPhone 更改为iPad,索引号 4 应该是不同的ViewControllers

例如

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

我在目标 c 中得到了答案,link here,但我无法为 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)

我们将不胜感激。

【问题讨论】:

  • 你的设置vc是不是也嵌入到tabbar中了?
  • @PankajGupta 不,ViewController 5 已嵌入,但我需要将其替换为该索引的另一个未嵌入的视图控制器。
  • @AbhishekMitra 检查我的答案

标签: ios iphone swift3 uitabbarcontroller


【解决方案1】:

在这里您可以更改特定索引的viewController,如下所示

在你的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)

【讨论】:

  • 抱歉回复晚了,它运行良好,非常感谢.. :)
  • 这里let newItemController = UIStoryboard(name: "Settings", bundle: nil).instantiateViewController(withIdentifier: "SettingsViewController") as! SettingsViewController newItemController.title = "Settings",我改了let newItemController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "SettingsViewController") as! SettingsViewController newItemController.title = "Settings"
  • 通常不这样做,但是......就像一个魅力!谢谢!
猜你喜欢
  • 2014-11-02
  • 2017-07-03
  • 1970-01-01
  • 1970-01-01
  • 2020-04-16
  • 1970-01-01
  • 2022-08-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多