Why is my custom delegate method not being called?

I have a viewController with 4 buttons (HomePage) and then a TabBarController with 3 viewControllers. One of the TabBarController's viewControllers I want to be used as a way to get back to the "HomePage" via a tabBar icon. I have associated a custom class that I created called "HomeViewController" to that viewController. See diagram below

在这里输入图像描述

HomeViewController .H file. I have created a protocol with a method "returnToHomepage"

在这里输入图像描述

HomeViewController .M file As soon as the view is loaded it calls the delegate. 在这里输入图像描述

In my HomepageViewController .H file I have made sure that the file adheres the protocol. 在这里输入图像描述

HomepageViewController .M file

I instantiate an instance of HomeViewController and set delegate to self but returnToHomePage method never gets called! Not sure what I'm missing... 在这里输入图像描述


I think that your're calling the delegate method before the delegate is set.

When you call alloc-init on the controller, it initializes and ViewDidLoad is called,... and THEN you set the delegate... so this

[self.delegate returnToHomepage];

is called before

homeVC.delegate = self;

The HomeViewController you're creating in viewDidLoad is not the same one that's actually being presented onscreen. You'll need to access it with your UITabBarController's viewControllers method and set it's delegate that way.

链接地址: http://www.djcxy.com/p/34834.html

上一篇: 使用带有自定义URL协议的NSOutputStream outputStreamWithURL

下一篇: 为什么我的自定义委托方法不被调用?