【问题标题】:View Controller disappearing after SegueSegue 后视图控制器消失
【发布时间】:2019-05-02 02:22:27
【问题描述】:

我有一个客户选项卡控制器,它有一个自定义图标,当用户单击弹出菜单时会出现 3 个选项。当我单击第一个选项时,它应该将我带到一个新的视图控制器,但是当我单击它时,视图控制器只会出现一秒钟,然后再次消失。我不知道为什么,但这是我的客户标签栏代码:

import UIKit
import PopMenu

class TabBarController: UITabBarController, UITabBarControllerDelegate {

    override func viewDidLoad() {
        delegate = self
    }

    func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
        if viewController.title == "for custom action" {
            let manager = PopMenuManager.default

            let action1 = PopMenuDefaultAction(title: "Scan Barcode", didSelect: { action in

                self.performSegue(withIdentifier: "showScanBarcode", sender: nil)
                print("\(String(describing: action.title)) is tapped")
            })

            let action2 = PopMenuDefaultAction(title: "Action 2", didSelect: { action in

                print("\(String(describing: action.title)) is tapped")
            })

            let action3 = PopMenuDefaultAction(title: "Action 3", image: UIImage(named: "wine"), didSelect: { action in
                print("\(String(describing: action.title)) is tapped")
            })

            manager.addAction(action1)
            manager.addAction(action2)
            manager.addAction(action3)

            manager.present()


            return false
        }
        return true
    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "mySegue" {

            let controller = segue.destination as! myViewController

            controller.navigationItem.leftBarButtonItem = splitViewController?.displayModeButtonItem
            controller.navigationItem.leftItemsSupplementBackButton = true

        }
    }

}

这是显示流程的图像。用户单击相机按钮,然后出现一个弹出菜单,当用户单击一个选项时,我想将它们带到一个新的视图控制器(未连接到标签栏控制器)。我设置了第一个链接到一个新的视图控制器,它显示了几秒钟然后消失了。

【问题讨论】:

  • showScanBarcode 做了什么样的转场(推送、弹出、呈现)?添加更多代码细节,例如故事板和showScanBarcodes viewcontroller 类
  • 这是一个 push segue,在 IB 中创建,标识符为 showScanBarcode ... scowScanBarcode 类现在是空的,它只是一个模板
  • TabBarController 是否嵌入在 UINavigationController 中以使 push segue 工作? “视图控制器只出现一秒钟,然后再次消失”请详细说明
  • 我在描述中添加了更多信息,TabBarController 没有嵌入到导航控制器中,但是 TBC 中的一些屏幕是嵌入的。当他们单击中间图标时,会出现一个自定义弹出窗口,我希望用户单击其中一个选项并被引导到与标签栏控制器无关的单独视图控制器,如果这有意义的话
  • @user2647092 我认为您需要将 (segue.identifier == "mySegue") 更新为 (segue.identifier == "showScanBarcode")

标签: ios swift segue


【解决方案1】:

你正在为 segue 方法使用不同的标识符,

performSegue(withIdentifier: "showScanBarcode", sender: nil)

prepare(for segue: UIStoryboardSegue, sender: Any?).

所以,请使用相同的标识符。 希望这会对你有所帮助。

【讨论】:

    【解决方案2】:

    这是 PopMenue 的问题。

    PopMenuManager 在最顶层的视图控制器上显示一个 UIViewController 并在选择后调用dismiss()。解雇通过所有控制器递归。当他这样做时,您的新视图控制器是否位于最顶层并被解雇。 在单独的线程中进行 segue 可能会有所帮助。 (可能还有短暂的测试延迟)

        let action1 = PopMenuDefaultAction(title: "Scan Barcode", didSelect: { action in
    
            DispatchQueue.main.async {
                self.performSegue(withIdentifier: "showScanBarcode", sender: nil)
            }
            print("\(String(describing: action.title)) is tapped")
        })
    

    【讨论】:

      【解决方案3】:

      看起来您将action1 存储在manager 中,并且这是此函数的本地内容。因此,在函数完成执行后,manager 及其内容会超出范围并被垃圾回收。

      要解决此问题,请将manager 声明为类中(函数外部)的实例变量。例如:

      class TabBarController: UITabBarController, UITabBarControllerDelegate {
         let manager = PopMenuManager.default
         ...
         func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
            ...
            manager.addAction(action1)
         }
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-02-17
        • 1970-01-01
        • 1970-01-01
        • 2021-10-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多