如何在 swift 3 中使用 UIView class 中的 performSegue

How to use performSegue from UIView class in swift 3

我有一个 UIView class,其中包含一个 UILabel。我已经为 UILabel 添加了一个 UITapGestureRecognizer,并想做一个 performSegue 以在点击 UILabel 时打开一个新的 UIViewController。 问题是我不能在 UIView class.

中使用 performSegue

谁能帮忙在UIViewclass中使用performSegue

我已将点击手势添加到我的标签中,

nameLabel.isUserInteractionEnabled = true

let tap = UITapGestureRecognizer(target: self, action: #selector(tapFunction))  
tap.numberOfTapsRequired = 1
tap.numberOfTouchesRequired = 1
tap.isEnabled = true
nameLabel.addGestureRecognizer(tap)

现在在我的 tapFunction 中,

func tapFunction(sender:UITapGestureRecognizer) {
    print("tap working")
    self.performSegue(withIdentifier: "memberSegue", sender: self)
}

但是我得到一个错误:

"Value of type UIViewController has no member performSegue"

因为它是一个 UIView Class。

我建议您为点击手势添加动作而不是 segue。

TapRecognizer.addTarget(self, action: #selector(YourFunc))

样本函数:

func YourFunc(){                  
        let storyBoardHome = UIStoryboard(name: "", bundle: nil)        
     let memberController = storyBoardHome.instantiateViewController(withIdentifier: "") 
           self.navigationController?.pushViewController(memberController, animated: true)
                }

参考这个:

你写的错误是错误的,UIViewController的确实有那个方法。您的错误必须是 "UIVIew has no member...",至于解决方案,您只需要获取对使用此特定视图的任何 UIViewController 的引用即可。

然后使用该引用调用您的执行方法。

例如,如果您的自定义视图是以编程方式添加的,您可以将额外的 属性 添加到您的 class like

weak var myVC : UIViewController?

然后在实例化后将其分配给此 属性。

然后执行转场变成:

myVC?.performSegue....

或者,您可以只从使用此视图的 UIViewController 添加点击手势识别器,然后让视图从那里显示它。


或者您可以只使用:

UIApplication.shared.keyWindow?.rootViewController.performSegue...

但如果这样做,您应该小心,因为这可能会产生意想不到的后果,具体取决于您呈现的内容以及当前的 rootViewController。

在 UIView 中创建委托 Class 并习惯于在 YourController 中使用 UIView class

 protocol UIViewDelegate { 
     func tapFunction() //this function will be use in controller class which uiview are using
 }

 var delegate: UIViewDelegate?

 func tapFunction(sender:UITapGestureRecognizer) {
     delegate?.tapFunction()
 }

 class YourController: UIViewController, UIViewDelegate {
     let yourView = YourView()
     yourView.delegate = self

     func tapFunction() {
         self.performSegue(withIdentifier: "memberSegue", sender: self)
     }
 }

您应该尝试将任何功能与您的 UIViews 分开。最佳做法是您的 UIViewControllers 负责您应用中的任何功能,而您的 UIViews 仅用于显示元素。因此,我建议您在 UIView 中将 nameLabel 设为 属性,这样您就可以在实例化 UIView 后从 UIViewController 访问它。那么 UIViewController 中的代码应该如下所示:

let yourView = new YourView()
let tap = UITapGestureRecognizer(target: self, action: #selector(tapFunction))
tap.numberOfTapsRequired = 1
tap.numberOfTouchesRequired = 1
tap.isEnabled = true
yourView.nameLabel.addGestureRecognizer(tap)

并且您将在 UIViewController 中拥有 tap() 功能。

你可以通过协议和委托来完成。因为实际上您不能从 UIView 执行此操作,所以您应该始终从控制器执行此操作。

/// add this protocol in your project
protocol ViewProtocol {
    func performSegueFromView()
}

class View : UIView {

    /// add this line to your view
    var delegate : ViewProtocol?


    /// replace your tap function with this
    func tapFunction(sender:UITapGestureRecognizer) {

        print("tap working")

        self.delegate?.performSegueFromView()
    }

}


class ViewController : UIViewController, ViewProtocol {

    override func viewDidLoad() {

        /// create view
        let view = View()
        /// assign view delegate
        view.delegate = self

    }

    /// delegate function
    func performSegueFromView() {
        ///peform segue from controller because you can not do this from uiview anyway
        self.performSegue(withIdentifier: "memberSegue", sender: self)
    }

}