简体   繁体   English

UITabBarControllerDelegate中的变量不会更新

[英]Variables don't update in UITabBarControllerDelegate

Here is what I'm trying to do: 这是我想要做的:

选单检视压缩机视图

When user clicks the 'Compressors' button, it shows a scroll view with details of compressors (or whatever). 当用户单击“压缩机”按钮时,它将显示一个滚动视图,其中包含压缩机(或其他内容)的详细信息。 And to get back to menu, user clicks the 'More' tab bar item. 然后返回菜单,用户单击“更多”选项卡栏项。

I've set up a variable first to store information if this is first time user taps the 'More' item. 如果这是用户首次点击“更多”项,则我first设置了一个变量来存储信息。

class MoreVC: UIViewController {

var first : Bool = true

@IBOutlet weak var scrollView: UIScrollView!

override func viewDidLoad() {
    super.viewDidLoad()

    scrollView.isHidden = true

}


open func hideEverything(_ bool : Bool)
{
    print(bool)
    if bool == true
    {
        // First time user taps More - nothing happens
        return
    }
    print("hideEverything()")

}

@IBAction func compressorsButton(_ sender: Any) {
    scrollView.isHidden = false
    print(first)
}

override func viewDidAppear(_ animated: Bool) {
    print("-> \(self.title!)")
    first = false
}

override func viewDidDisappear(_ animated: Bool) {
    scrollView.isHidden = true
    first = true
}

}

Then I have Tab bar delegate in different file: 然后我在其他文件中有标签栏委托:

class myTabBarController: UITabBarController, UITabBarControllerDelegate
{
override func viewDidLoad() {
    self.delegate = self
}



func tabBarController(_ tabBarController: UITabBarController, didSelect selectedViewController: UIViewController) {
    if selectedViewController is MoreVC
    {
        let more = MoreVC()
        print("more.first = \(more.first)")
        if more.first == false
        {
            more.scrollView.isHidden = true
        }
    }
}
}

So, when I tap compressors button, view shows up, when I switch tabs, view hides. 因此,当我点击“压缩机”按钮时,将显示视图,当我切换选项卡时,将隐藏视图。 But when scrollView is up and I press 'More', it doesn't trigger the more.scrollView.isHidden = true because it detects that first = true , which it is not! 但是当scrollView启动并且按“更多”时,它不会触发more.scrollView.isHidden = true因为它检测到first = true ,但不是! It's set to false (as it should be), but I still get true there. 它设置为false (应该是),但在那里我仍然是true I think what I need to do is somehow update myTabBarController , but I have no idea how. 我认为我需要做的是以某种方式更新myTabBarController ,但我不知道如何。

Please help, any hint on what is going on will be much appreciated! 请帮忙,任何关于正在发生的事情的提示将不胜感激! Also thanks for reading this far, I hope you understood my problem. 也感谢您阅读本文,希望您理解我的问题。

EDIT from future me: 从未来我编辑:

This problem could have been resolved more naturally by using navigation controller through my app (which I ended up doing anyway). 通过在我的应用程序中使用导航控制器(我最终还是这样做了),可以更自然地解决此问题。 Joe's answer is correct, but if someone bumps into same problem as me, there is a second way. 乔的回答是正确的,但是如果有人遇到与我相同的问题,那还有第二种方法。

Since you are updating the boolean on the newly created MoreVC, the existing one doesn't change at all. 由于您正在更新新创建的MoreVC上的布尔值,因此现有的布尔值完全不变。

Instead of: 代替:

if selectedViewController is MoreVC

update it to: 更新到:

if let more = selectedViewController as? MoreVC

then get rid of the line where you create a brand new MoreVC. 然后摆脱创建全新的MoreVC的路线。

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

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