2

我有一个UIView包含UILabel. 我已经添加了一个UITapGestureRecognizerUILabel并且想要做一个performSegue来打开一个新UIViewControllerUILabel。问题是我不能performSegueUIView课堂上使用。

有人可以帮忙performSegueUIView课堂上使用吗?

我在我的标签上添加了一个点击手势,

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)
}

但我收到一个错误:

“UIViewController 类型的值没有成员 performSegue”

因为它是一个 UIView 类。

4

5 回答 5

3

您应该尝试将任何功能与您的UIViews. 最佳实践是您UIViewControllers对应用程序中的任何功能负责,并且UIViews仅用于显示元素。因此,我建议您在您的 中创建nameLabel一个属性UIView,以便UIViewController在实例化UIView. 然后你的代码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)

你会tap()在你的UIViewController.

于 2017-02-23T08:36:27.187 回答
1

你可以通过协议和委托来做到这一点。因为实际上你不能从 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)
    }

}
于 2017-02-23T08:54:00.887 回答
1

在 UIView 类中进行委托,并习惯于在 YourController 类中使用 UIView

 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)
     }
 }
于 2017-02-23T08:07:52.797 回答
0

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

然后使用该引用来调用您的 perform 方法。

例如,如果您的自定义视图是以编程方式添加的,您可以向您的类添加一个额外的属性,例如

weak var myVC : UIViewController?

然后在实例化它之后将它分配给这个属性。

然后 perform segue 变为:

myVC?.performSegue....

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


或者你可以只使用:

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

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

于 2017-02-23T08:03:45.520 回答
0

我建议您为该点击手势添加动作而不是 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)
                }

请参考:https ://stackoverflow.com/a/26366366/6818278

于 2017-02-23T07:13:21.727 回答