ios - 从 UIView 执行 segue

标签 ios objective-c uiview segue uistoryboardsegue

我有 ViewController,里面有 UIView。

这个 UIView 有单独的 myView 类,并且有许多 UI 元素 - 其中之一是 CollectionView。

我想要的是在选择 myView 中的集合元素之一时执行 segue。但是当我尝试添加行时

performSegue(withIdentifier: "myIdintifier", sender: self)

到集合的 View didSelectItemAt 方法我得到错误

Use of unresolved identifier 'performSegue'



而且我知道这是因为我在扩展 UIView 而不是 UIViewController 的类中执行此操作。

那么在这种情况下我该如何执行 segue 呢?还有我该如何准备segue?

最佳答案

在这里,我将逐步评估它。

步骤 - 1

使用下面的协议(protocol)创建自定义委托(delegate)将指导您自定义 UIView。 protocol必须存在于您的自定义 View 范围之外。

protocol CellTapped: class {
    /// Method
    func cellGotTapped(indexOfCell: Int) 
}

不要忘记在您的自定义 View 上创建上述类的委托(delegate)变量,如下所示
var delegate: CellTapped!

使用您的 Collection View didSelect 方法,如下所示
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        if(delegate != nil) {
            self.delegate.cellGotTapped(indexOfCell: indexPath.item)
        }
    }

步骤 - 2

让我们来看看你的 View Controller 。给 CellTapped到您的 View Controller 。
class ViewController: UIViewController,CellTapped {

    @IBOutlet weak var myView: MyUIView! //Here is your custom view outlet
    override func viewDidLoad() {
        super.viewDidLoad()
        myView.delegate = self  //Assign delegate to self
    }

    // Here you will get the event while you tapped the cell. inside it you can perform your performSegue method.
    func cellGotTapped(indexOfCell: Int) {
        print("Tapped cell is \(indexOfCell)")
    }
}

希望这会帮助你。

关于ios - 从 UIView 执行 segue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45780752/

相关文章:

ios - 检测点击包含多个部分的 UITableView 的 UITableViewCell 中的按钮

objective-c - 拖动文件遇到沙箱(__CFPasteboardIssueSandboxExtensionForPath)

objective-c - 同时在多个 UIView 上接收触摸事件

ios - 检测何时将我的 UIView 子类添加到另一个 UIView

ios - 为什么我的框架包含 .framework 文件时出错

ios - 将 Apple SCN 文件批量转换为 DAE

iphone - 在应用程序不运行期间,如何增加applicationIconBadgeNumber的值?

ios - 带有 UIPanGestureRecognizer 的 UIView 动画速度太快(不减速)

ios - 无法更改 UIView 的框架属性

ios - 如何在展开功能中执行segue