如何调用委托方法

时间:2014-07-23 05:55:27

标签: ios iphone objective-c ios7 delegates

如何委托实际调用方法?

比如说我把它添加到我的UIViewController.m:

- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    // code
}

视图控制器出现时是否调用该方法?代表团如何运作?

3 个答案:

答案 0 :(得分:2)

如果将对象设置为导航控制器的委托,则导航控制器将在视图控制器即将出现时调用此方法。

有关委托方法实施的详细信息,请参阅this questionConcepts in Objecctive-C Programming: Delegates and Data Sources

答案 1 :(得分:2)

根据文档,在导航控制器显示viewController’s视图和navigationItem属性之前调用代理。

但是,看来你的问题是,“怎么样?”。

在您的情节提要或代码中的某处,UINavigationController已设置完毕。无论在哪里,它都有一个委托属性(一个变量)。该属性设置为实现UINavigationControllerDelegate协议的某个对象。

例如:

MySpecialViewController *myViewController = [[MySpecialViewController alloc] init];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:myViewController];
navigationController.delegate = self;

现在,只要navigationController即将呈现视图控制器,就会在您的对象上调用navigationController:willShowViewController:animated:委托方法。

答案 2 :(得分:1)

UINavigationController类中可能是这样的:

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
   [self.delegate navigationController:self willShowViewController:viewController animated:animated];

   //do stuff to actually push the view controller
}

如果委托是零,则没有任何反应,因为对nil的消息不起作用,但是如果设置委托,则调用该方法。我不确定它是否直接在pushViewController:方法中发生,但这并不重要。每当导航控制器即将显示下一个视图控制器时,它都会向其代理发送一条消息

相关问题