ios - 如何调用委托(delegate)方法

标签 ios iphone objective-c ios7 delegates

如何委托(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/

相关文章:

iPhone 故障保护多个文件下载

iphone - 具有自定义上下文菜单选项的 iOS 浏览器应用程序

ios - iOS 中的 NSFaultingOrderedSet

ios - 如何在左侧设置 UISegmentedControl 文本

ios - 具有设备密码功能的 Touch Id 实现

c++ - 代码错误 "ld: library not found for -lmysqlclient "

objective-c - 如何在忽略大小写的情况下最好地比较两个 NSString 对象?

ios - PubNub 注册 channel 后没有收到推送通知?

ios - 将 ADBannerView 添加到 SKScene

iphone - iPhone 上的 SQLite 访问速度慢