如何知道哪个视图控制器呈现了一个视图控制器

时间:2016-10-14 09:40:04

标签: ios swift

我有几个视图控制器('从'视图控制器)连接到一个视图控制器ItemCollectionVC
当显示ItemCollectionVC时,我想知道哪个视图控制器呈现它。< br />
怎么做?

'来自'视图控制器:

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

编辑:

我正试图以这种方式访问​​该属性:

 switch self.presentingViewController!.title! {
 // Error
    case "CounterBuildVC":
        dataSourceItems = counterBuildItems
    case "FreeBuildVC":
        dataSourceItems = freeBuildItems
    case "KrakenViewController":
        dataSourceItems = captureKrakenItems
    default:
        break
    }
  

然而,它崩溃了错误:意外地发现没有,我确定我设置了   故事板中的标题

4 个答案:

答案 0 :(得分:2)

UIViewController的{​​{3}}属性是您正在寻找的东西

答案 1 :(得分:1)

我这样做的方法就是在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

答案 2 :(得分:0)

您可以使用self.presentingViewController

获取视图控制器

答案 3 :(得分:0)

如果您从ViewControllerA提供了ViewControllerB。然后在ViewControllerB中,您可以通过以下代码检查呈现的ViewController

if([self.presentingViewController isKindOfClass:[ViewControllerA class]]){ 
               /*Write your Code here*/
}