Accessing parent view controller (custom) properties

后端 未结 3 1059
死守一世寂寞
死守一世寂寞 2020-12-24 08:40

I have written a UITabBarController subclass (called MainViewController) and added a few instance variables, specifically a venue of type Venue. Ea

3条回答
  •  南笙
    南笙 (楼主)
    2020-12-24 09:24

    You're doing something completely wrong. parentViewController is declared as a UIViewController. NSLoging it outputs its real type due to the wonders of polymorphism.

    UIViewController doesn't have a Venue member. Your MainViewController does. Casting it is, in fact, the right answer. Make sure to import MainViewController.h as well.

    #import "MainViewController.h"
    [...]
    NSString *name = ((MainViewController *)self.parentViewController)).venue.name;
    

    Also make sure, of course, that venue is declared as a @property of MainViewController, and name is a @property of Venue.

提交回复
热议问题