开发者

Accessing UIView methods

开发者 https://www.devze.com 2023-01-11 08:35 出处:网络
I have a UIViewController that contains a UITabBarController, which contains a UIViewController, e.g.:

I have a UIViewController that contains a UITabBarController, which contains a UIViewController, e.g.:

UIViewController1 -> UITabBarController -> UIViewController2

I want to hook up a button in UIViewController2 that will call a method in UIViewController1, but how do I access UIViewControlle开发者_Go百科r1 from UIViewController2? I know that calling self.parentViewController from UIViewController2 gets me to the UITabBarController, but where do I go from there?

I tried self.parentViewController.parentViewController from UIViewController2, but got null.

Any suggestions?


The self.parent.parent technique often won't work because there may be Apple implementation layers in-between your controllers. Best to create pointers yourself in your own implementation by subclassing and creating instance variables, and setting them at the point of creation.


There are several ways to do this depending on what your actually trying to solve....for example if its a utility like class method then you should put it in a different file and import it into UIViewController2. If its an instance method then you obviously have an instance of UIViewController1 then you need to declare an instance variable of type UIViewController1 that holds a reference to the view controller. Then you can call methods on that instance....what is it that you are trying to accomplish?


Just add the first one as a property and assign it when you instantiate:

viewController1.otherView = viewController2;
0

精彩评论

暂无评论...
验证码 换一张
取 消