简体   繁体   English

可以使用xib手势识别器进行segue吗?

[英]possible to segue with xib gesture recognizer?

I am trying to go to a different segue if the user clicks on an element like imageview from the xib (via gesture recognizer) instead of the rest of the row in a tableView (since that is a different segue).如果用户从 xib(通过手势识别器)而不是 tableView 中的行的其余部分(因为这是一个不同的 segue)点击像 imageview 这样的元素,我会尝试转到不同的 segue。 I have seen people do it with objective C, but I am not sure how to do it in Swift, has anyone done it before successfully?我见过有人用目标 C 做这件事,但我不知道如何在 Swift 中做到这一点,有没有人成功地做到过? I know I would have to make the code changes on the custom xib file and then get the actual controller to make the transition (if I am to go by the objective C way).我知道我必须对自定义 xib 文件进行代码更改,然后让实际控制​​器进行转换(如果我要按照目标 C 方式进行)。

So if you have a UIButton in your cell, you can use the below code to performSegue on its click.因此,如果您的单元格中有 UIButton,则可以使用以下代码在单击时执行转场。

   func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell : YourCell = tableView.dequeueReusableCell(withIdentifier: "YourCellIdentifier", for: indexPath) as! YourCell
    cell.imageButton.addTarget(self, action: #selector(imageTapped(sender:)), for: .touchUpInside)
    return cell
}


@objc func imageTapped(sender : UIButton) {
    self.performSegue(withIdentifier: "YourIdentifer", sender: self)
}

So in cellForRowAt, we are assigning your imageButton a function that will be called whenever the button is clicked ie imageTapped()因此,在 cellForRowAt 中,我们为您的 imageButton 分配了一个函数,该函数将在单击按钮时调用,即 imageTapped()

As your imageTapped function is in your viewController, you can easily perform a segue by creating one from VC1 to VC2由于您的 imageTapped 函数在您的 viewController 中,您可以通过从 VC1 到 VC2 创建一个来轻松执行转场

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM