From within a view controller in a container view, how do you access the view controller containing the container?

前端 未结 7 2053
南笙
南笙 2020-12-28 13:14

This is tricky to word but I have a view controller (vc1) that contains a container view (I\'m using storyboards). Within that container view is a navigation controller and

7条回答
  •  时光取名叫无心
    2020-12-28 13:20

    1) on VC2 expose a property for passing in a reference to VC1

    //VC2.h
    #import "VC1.h"
    
    @interface VC2 : NSObject
    @property (strong, nonatomic) VC1 *parent;
    @end
    

    2) on VC1, pass self into the property exposed in VC2 in your prepareForSegue method after you setup your segue's identifier to "ToVC2". Then pass the reference like so:

    //VC1.m
    @implementation VC1 
    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if([segue.identifier isEqualToString:@"ToVC2"]) {
        VC2 *vc2 = segue.destinationViewController;
        vc2.parent = self;
    }
    }
    

提交回复
热议问题