【问题标题】:How do I check the currently loaded view controller ? (ObjC + Swift)如何检查当前加载的视图控制器? (ObjC + 斯威夫特)
【发布时间】:2018-10-23 07:47:30
【问题描述】:

在我的应用程序中,我有一个函数,我希望它根据加载的当前视图控制器打印一些东西。我通过设置一个全局变量(布尔)然后切换视图控制器类中的标志来做到这一点。在我的主要课程中,我有这样的东西:

var FirstViewControllerisVisible: Bool = false
var SecondViewControllerisVisible: Bool = false
var ThirdViewControllerisVisible: Bool = false

@objc func PlayAgainfunc(_ sender: Any) {
    if counter % 15 == 0 {
        if FirstViewControllerisVisible == true {
            print("First View Controller is visible")
        } else if SecondViewControllerisVisible == true {
            print("Second View Controller is visible")
        } else if ThirdViewControllerisVisible == true {
            print("Third View Controller is visible")
        }
    }
    counter += 1
}

然后,在这些类中,我可以像这样设置标志:

override func viewDidAppear(_ animated: Bool) {
    FirstViewControllerisVisible = true
}

override func viewDidDisappear(_ animated: Bool) {
    FirstViewControllerisVisible = false
}

当它专门用于 Swift 时,这很有效,但全局变量的问题是它们不能被 Objective-C 访问。我找不到在我的 ObjC 类中设置标志然后检查真假的方法。例如,如果我尝试:

- (void)viewDidAppear:(BOOL)animated; {
    [(FirstViewControllerisVisible) == true];
}

- (void)viewDidDisappear:(BOOL)animated; {
    [(FirstViewControllerisVisible) == false];
}

我会收到错误“使用未声明的标识符”,因为我在 Swift 中声明的全局变量仅是 Swift。

【问题讨论】:

  • Swift Tips: Swift 变量和函数名是lowerCamelCase,所以firstViewControllerisVisibleplayAgainfunc。另外,不要将func 添加到函数名的末尾……所以只需playAgain。并且不需要包含the == true,所以if firstViewControllerisVisible {等。
  • 另外……[(FirstViewControllerisVisible) == true]; 完全错了。 == 是比较,而不是分配,为什么要包围在 [ ] 中?
  • 我需要把 == true 因为 firstViewControllerisVisible 是一个全局变量 Boolean 我组成。默认情况下它是假的,当 viewdidappear 被触发时,标志被设置为真。另外,我猜 Objective-C 是错误的,我不像 Swift 那样精通。我试图在 Objective-C 中做我在 Swift 中一直在做的事情,但是在 swift 中声明的全局变量不能被 ObjC 使用。这就是我想要弄清楚的,如何在 Objective-C 中设置一个标志来读取 VC 是否可见,然后在 Swift 中执行基于该标志的函数。

标签: objective-c swift uiviewcontroller global-variables


【解决方案1】:

视图控制器加载完成后,您可以

if let viewController = UIApplication.shared.keyWindow?.rootViewController {
    print(type(of: viewController))
}

【讨论】:

    【解决方案2】:

    添加下面提到的扩展名。你会得到想要的结果。

    extension UIApplication {
            /// will return currently showing view controller
            static var topMostViewController: UIViewController? {
                return UIApplication.shared.keyWindow?.rootViewController?.visibleViewController
            }
        }
    
        extension UIViewController {
            /// The visible view controller from a given view controller
            var visibleViewController: UIViewController? {
                if let navigationController = self as? UINavigationController {
                    return navigationController.topViewController?.visibleViewController
                } else if let tabBarController = self as? UITabBarController {
                    return tabBarController.selectedViewController?.visibleViewController
                } else if let presentedViewController = presentedViewController {
                    return presentedViewController.visibleViewController
                } else {
                    return self
                }
            }
        }
    

    并像这样访问它:

    let viewController = UIApplication.topMostViewController
    

    快乐编码

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-27
      • 1970-01-01
      • 2021-03-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多