【问题标题】:Dismiss previous ViewController upon selecting UITabBarItem for second time第二次选择 UITabBarItem 时关闭以前的 ViewController
【发布时间】:2018-07-16 12:30:18
【问题描述】:

我在一个 iOS 应用程序上工作,该应用程序实现了一个连接了 5 个图标的 UITabBarController。整个项目是通过 Storyboard 制作的。因为我想为用户提供 4 个主题的选项,我在静态 tableView 中做的事情,我遇到了一个问题。

当用户更改主题时,先前通过 tabBar 视图控制器实例化的视图控制器不会更新到主题更改,因为它们在主题更新之前存在。我可以以某种方式解除它们并在 UITabBar 委托的 didSelect 方法上创建相同视图控制器类的新实例吗?

我是否正确地处理了这个问题?任何帮助将不胜感激!

提前感谢您的帮助。

类 MainTabController: UITabBarController {

override func viewDidLoad()
{
    super.viewDidLoad()
}



override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem)
{
    let theme = UserDefaults.standard.string(forKey: "selectedColor")
    print(theme!)
    for vc in self.viewControllers!
    {


        if (theme == "default")
        {
            vc.view.backgroundColor = UIColor(red: 0xF9 , green: 0xF7 , blue: 0xF7)

        }else if (theme == "theme1")
        {
            vc.view.backgroundColor = UIColor(red: 0x1F , green: 0xFF, blue: 0xFF)

        }else if (theme == "theme2")
        {
            vc.view.backgroundColor = UIColor(red: 0xE3 , green: 0xFD, blue: 0xFD)

        }else if (theme == "theme3")
        {
            vc.view.backgroundColor = UIColor(red: 0x3E, green: 0xC1, blue: 0xD3)
        }

    }

}

}

在每个视图控制器中的实现示例:

override func viewDidLoad()
{
    let theme = UserDefaults.standard.string(forKey: "selectedColor")
    print(theme!)
    if (theme == "default")
    {
        outerView.backgroundColor = UIColor(red: 0xF9 , green: 0xF7 , blue: 0xF7)

    }else if (theme == "theme1")
    {
        outerView.backgroundColor = UIColor(red: 0x1F , green: 0xFF, blue: 0xFF)

    }else if (theme == "theme2")
    {
        outerView.backgroundColor = UIColor(red: 0xE3 , green: 0xFD, blue: 0xFD)

    }else if (theme == "theme3")
    {
        outerView.backgroundColor = UIColor(red: 0x3E, green: 0xC1, blue: 0xD3)
    }
}

【问题讨论】:

  • 显示一些知道你在代码中做什么的代码
  • 如果您可以存储更改并使其反映在 viewDidAppear 方法块中,请使用单吨类
  • 你是如何让你的主题在视图控制器中生效的。解决方案可能取决于它。请出示您的代码。

标签: ios iphone swift xcode swift4


【解决方案1】:

didSelect 中,您可以访问选项卡 VC 并根据需要更改它们(在选项卡中的任何 VC 中)

for vc in self.tabBarController!.viewControllers! {

   vc.view.backgroundColor = UIColor.red

  // if you want to set a property 

  if let other  = vc as? OtherViewController {
     // change here 
  }  

}

//

如果您希望在 tabBar 自定义类中使用此代码

 for vc in self.viewControllers! {

   vc.view.backgroundColor = UIColor.red

  // if you want to set a property 

  if let other = vc as? OtherViewController {
     // change here 
  }  

}

//

编辑后,您需要创建一个包含 outerView 的协议并在每个 VC 中遵守此协议,或者转换为 VC 并更改其属性

【讨论】:

  • 我的知识不足以使用协议。你能给我举个第二选择的例子吗?
【解决方案2】:

创建自定义 UITabBarController 类并在其中添加 viewDidLoad

for vc in self.viewControllers! {

   vc.view.backgroundColor = .....

}  

现在将此自定义类链接到 Storyboard 的 UITabBarController

更新:

您从 UserDefaults 获取的值不正确。

UserDefaults.standard.string(forKey: "selectedColor")  

应该是:

UserDefaults.standard.object(forKey: "selectedColor")

【讨论】:

    【解决方案3】:

    我的问题的解决方案非常简单,但需要经过数小时的搜索!

    我不得不将编辑视图的所有代码都放在 viewDidAppear 而不是 viewDidLoad 中,因为 viewDidLoad 只在第一次运行时才运行 每次用户按下此特定选项卡时都会调用 viewDidAppear。

    感谢大家的快速解答!

    【讨论】:

      猜你喜欢
      • 2017-12-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-29
      • 1970-01-01
      • 2021-03-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多