如何委托(delegate)方法实际被调用?
假设我将其添加到我的 UIViewController.m 中:
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
// code
}
当 View Controller 出现时是否调用该方法?委派如何运作?
最佳答案
根据文档,在导航 Controller 显示 viewController 的
View 和 navigationItem
属性之前调用委托(delegate)。
但是,您的问题似乎是“如何?”。
在您的 Storyboard 或代码中的某处,设置了 UINavigationController
。无论在哪里,它都有一个委托(delegate)属性(一个变量)。该属性设置为实现 UINavigationControllerDelegate
协议(protocol)的某个对象。
例如:
MySpecialViewController *myViewController = [[MySpecialViewController alloc] init];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:myViewController];
navigationController.delegate = self;
现在,每当 navigationController
将呈现一个 View Controller 时,navigationController:willShowViewController:animated:
委托(delegate)方法将在您的对象上被调用。
关于ios - 如何调用委托(delegate)方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24902884/