Segue

时间:2019-05-02 02:22:28

标签: ios swift segue

我有一个customer选项卡控制器,该控制器具有一个自定义图标,当用户单击弹出菜单时会显示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

        }
    }

}

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

enter image description here

3 个答案:

答案 0 :(得分:5)

您将不同的标识符用于segue方法

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

AND

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

因此,请使用相同的标识符。 希望对您有帮助。

答案 1 :(得分:0)

似乎您将action1存储在manager中,并且此函数是本地的。因此,函数完成执行后,manager及其内容将超出范围并收集垃圾。

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

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

}

答案 2 :(得分:0)

那是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")
    })
相关问题