我有 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/