2016-10-14 14 views
0

1つのビューコントローラに複数のビューコントローラ( 'From'ビューコントローラ)を接続しましたItemCollectionVC
ItemCollectionVCが提示されたとき、どのView Controllerがそれを提示したのか知りたい。

どうすればいいですか?ビューコントローラ 'から'どのビューコントローラがビューコントローラを提示したかを知る方法

@IBAction func selectOpponentItems(_ sender: UIButton) { 

    let VC = storyboard?.instantiateViewController(withIdentifier: "ItemCollectionVC") as! ItemCollectionVC 
    VC.preferredContentSize = CGSize(width: UIScreen.main.bounds.width/2, height: UIScreen.main.bounds.height/1.5) 
    VC.modalPresentationStyle = UIModalPresentationStyle.popover 
    VC.popoverPresentationController?.sourceView = sender 
    VC.popoverPresentationController?.sourceRect = sender.bounds 

    self.present(VC, animated: true, completion: nil) 
} 

ItemViewController:

override func viewDidLoad() { 
    super.viewDidLoad() 
    self.collectionView!.register(UICollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier) 
} 

編集:

私は財産をこのようにアクセスしようとしている:

However, it crashes with error: unexpectedly found nil, I'm sure I set the title in storyboard

+0

これは 'self.presentingViewController'メソッドで実現できます。 –

+0

[アクセスモーダルビューコントローラの親]の複製の可能性があります(http://stackoverflow.com/questions/6735170/access-modal-view-controller-parent) – Larme

答えて

1

presentingViewControllerプロパティは、あなたが、私はこのような何かに取り掛かるだろうな方法は、単に作成することですself.presentingViewController

+0

私がそれを実行したときにそのプロパティはゼロではないようでした –

+0

更新を参照してください –

+0

vcが表示されなくなるまで、このプロパティは表示されないようですので、viewWillAppear/viewDidAppearでこのプロパティを使用できます。 –

0

探しているものですItemCollectionVCヘッダーのデリゲート:

@property (nonatomic, assign) id delegate; 

セット:

VC.delegate = self; 

次に、ItemCollectionVCコントローラ内で、self.delegateを呼び出して、提示したビューコントローラから情報を取得できます。私はこれが助けることを願っています

乾杯!

ご注意:

self.presentingViewController; //This would be nil in viewDidLoad, so yeah not completely useful here 
1

を使用してビューコントローラを取得することができます

+0

これは本当にクールな方法ですが、本当に理解していない –

0

あなたはViewControllerAからViewControllerBを提示した場合。 ViewControllerBでは、表示されたViewControllerを次のコードで確認できます。

if([self.presentingViewController isKindOfClass:[ViewControllerA class]]){ 
       /*Write your Code here*/ 
} 
関連する問題