2017-08-20 6 views
0

私はViewControllerを持っており、その中にUIViewがあります。segueをUIViewから実行

このUIViewには別のクラスmyViewがあり、多くのUI要素があります.1つはCollectionViewです。

私が望むのは、myViewのコレクション要素の1つが選択されているときにsegueを実行することです。私はコレクションのビューdidSelectItemAt方法にライン

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

を追加しようとすると、しかし、私は未解決の識別子「performSegue」

のエラー

使用を取得し、私はこれがあるためであることを理解します私はUIViewを拡張し、UIViewControllerを拡張しないクラスの内部でそれを行います。

この場合、どのようにしてsegueを完成させることができますか?また、どのようにしてセグーの準備をすることができますか?

+0

カスタムデリゲートを使用してUIViewControllerでイベントをトリガーし、performSegue –

+0

を使用すると、より詳細な例を回答として提供できますか? – moonvader

答えて

1

ここでは、段階的に評価します。

ステップ - 1

スニペット以下のようなプロトコルを使用してカスタムデリゲートを作成し、カスタムUIViewの上でご案内します。 がカスタム表示スコープ外に存在する必要があります。

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

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { 
     if(delegate != nil) { 
      self.delegate.cellGotTapped(indexOfCell: indexPath.item) 
     } 
    } 

ステップ以下のようにあなたのコレクションビューdidSelect方法で行くにあなたのカスタムビューに以下のように

var delegate: CellTapped! 

を上記のクラスのデリゲート変数を作成するのを忘れてはいけません - 2

あなたのビデオに来てみましょうwコントローラ。あなたのviewcontrollerにCellTappedを与えてください。

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

希望すると、これが役に立ちます。

+0

ありがとう!驚くばかり :) – moonvader

1

プロトコル/代理人を使用して達成できます。

// At your CustomView 

protocol CustomViewProtocol { 
    // protocol definition goes here 
    func didClickBtn() 
} 


var delegate:CustomViewProtocol 




@IBAction func buttonClick(sender: UIButton) { 
    delegate.didClickBtn() 
    } 




//At your target Controller 
public class YourViewController: UIViewController,CustomViewProtocol 

let customView = CustomView() 
customView.delegate = self 

func didClickSubmit() { 
    // Perform your segue here 
} 
+0

ありがとうございました! – moonvader

関連する問題