gpt4 book ai didi

ios - 在 Extended 和 mainContorllers 中调用相同的委托(delegate)函数

转载 作者:行者123 更新时间:2023-12-01 18:58:06 25 4
gpt4 key购买 nike

我有一个带有 alertDelegate 功能的 BaseController

这是警报 View

@interface BaseController : UIViewController

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"TITLE" message:message delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];

假设我有另一个 ViewCONtorller 扩展了我的 BaseController
@interface MainController : BaseController

我面临的问题是,当我在 BaseController 中调用 AlertView 时,如果我的 MainController 有 alertDelegate
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
}

alertView 委托(delegate)仅在 MainController 中调用,而不在 BaseController 中调用。

我想如何在扩展 BaseController 的每个屏幕中执行此警报。然而,在每个 Controller 中复制粘贴相同数量的代码非常困难。由于每个 Controller 都扩展了它,有什么我可以在 BaseController 中调用它的吗?

最佳答案

您必须明确调用 super委托(delegate)方法:

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
[super alertView:alertView clickedButtonAtIndex:buttonIndex];
// Do other stuff
}

这是因为您已经对 View Controller 类进行了子类化,因此调用了任何被覆盖的方法而不是父类(super class)方法。这包括任何协议(protocol)方法,因为它们被认为没有什么不同。

关于ios - 在 Extended 和 mainContorllers 中调用相同的委托(delegate)函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25157019/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com