【问题标题】:Why does my ViewController get modally presented two times in a row after a single call to present(_:animated:completion:) method?为什么我的 ViewController 在一次调用 present(_:animated:completion:) 方法后连续两次模态显示?
【发布时间】:2019-06-13 11:20:23
【问题描述】:

当我尝试从 TabBarController 的第 4 个选项卡(嵌入在 NavigationViewController 中的 TableViewController)模态显示视图控制器时,它会连续显示两次。 实际方法present(_:animated:completion:) 连续调用两次,而它应该只发生一次。

这就是我从UITableViewController 调用方法的方式。现在因为我收到“尝试呈现其视图不在窗口层次结构中的 vc”警告,所以我尝试了这种解决方法,但我不再收到该警告,但现在我遇到了这个问题。

((UIApplication.shared.keyWindow?.rootViewController as? MainTabBarViewController)?.selectedViewController as? NavigationPodesavanjaViewController)?.visibleViewController?.present(Egg, animated: true, completion: nil)

这是视图控制器中呈现的所有内容。它现在就像一个虚拟内容,带有一个后退按钮:

import UIKit

class EasterEggViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
    view.backgroundColor = ConstantsClass.ljubicastaBoja

    let imageView = UIImageView(image: UIImage(named: "operator-ikonica"))
    view.addSubview(imageView)

    imageView.translatesAutoresizingMaskIntoConstraints = false
    imageView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
    imageView.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
    imageView.frame.size = CGSize(width: 250, height: 250)

    let backButton = UIButton()
    view.addSubview(backButton)

    backButton.setTitle("Nazad", for: .normal)
    backButton.titleLabel?.textColor = .white
    backButton.frame = CGRect(x: 50, y: 50, width: 100, height: 20)
    backButton.titleLabel?.adjustsFontSizeToFitWidth = true
    backButton.addTarget(self, action: #selector(dismissAction), for: .touchUpInside)

}

@objc func dismissAction() {
    dismiss(animated: true, completion: nil)
}


}

在长按手势的处理程序中调用 present 方法。 //注意:当我将长按更改为点击时,它可以正常工作,当我将其更改回来时,它再次出现两次。

这是来自 viewDidLoad 的部分代码:

    let longPressEgg = UILongPressGestureRecognizer()
    longPressEgg.addTarget(self, action: #selector(easterEggScreenPresent))
    easterEgg.addGestureRecognizer(longPressEgg)

这是处理程序:

 @objc func easterEggScreenPresent(){

    let Egg = EasterEggViewController()
    ((UIApplication.shared.keyWindow?.rootViewController as? MainTabBarViewController)?.selectedViewController as? NavigationPodesavanjaViewController)?.visibleViewController?.present(Egg, animated: true, completion: nil)
}

【问题讨论】:

  • 你确定在其他地方没有第二次出席电话吗?
  • 这是 tabBar 的根 viewController 吗?或者它是一个在推送后出现在第 4 个选项卡上的控制器?
  • @AndréSlotta 是的,同样的方法,在同一行,被调用了两次。
  • @GaloTorresSevilla 这在 TabBarViewController 的第 4 个选项卡上调用。
  • 我明白这一点。但这不是我的意思。您确定在您的代码的其他部分没有其他方法调用当前方法吗?顺便说一句,Egg 是什么?

标签: ios swift uitableview uiview


【解决方案1】:

UILongPressGestureRecognizer 以不同的状态被多次调用。在您的情况下,您应该在手势开始时显示视图控制器。将您的 easterEggScreenPresent 更改为以下内容:

@objc func easterEggScreenPresent(sender: UILongPressGestureRecognizer) {
    guard sender.state == .began else { return }

    let egg = EasterEggViewController()
    ((UIApplication.shared.keyWindow?.rootViewController as? MainTabBarViewController)?.selectedViewController as? NavigationPodesavanjaViewController)?.visibleViewController?.present(egg, animated: true, completion: nil)
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-10
    • 2021-12-15
    • 1970-01-01
    相关资源
    最近更新 更多