gpt4 book ai didi

ios - Objective-C:在子类中使用继承和调用方法

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:02:03 26 4
gpt4 key购买 nike

我正在设置继承。

我有一个父类,它有一个调用 block 的方法。一旦 block 返回,我需要它在子类中调用一个覆盖的方法。

当我设置断点时,如果失败,该 block 将调用父类中的方法。我究竟做错了什么?谢谢。

父类

.h

@interface BaseClass : UITableViewController
-(void)userModelUpdated
-(void)updateModel
@end

.m

@implementation BaseClass

-(void)userModelUpdated
{} //this is left blank intentionally as a hook


-(void)updateModel
{
//Here is s Block
[EndPoint updateUserModel:self.userModel successBlock:^{
//Do Something
} errorBlock:^(NSError *error, NSArray *errorArray) {
[self userModelUpdated]; // I want to call the method in the child class
// but when I setup the break points, it calls the method in the parent class
}
}

子类

.h

@interface childClass : BaseClass

.m

@implementation ChildClass

-(void)viewWillAppear:(BOOL)animated
{
[self updateModel];
}

-(void)userModelUpdated
{
// update UILabels Here
}

最佳答案

如果调用 updateModelselfChildClass 的实例,则 ChildClass 的 -userModelUpdated 实现将被执行

如果不是,那是因为您可能有一个 BaseClass 的实例,或者您有拼写错误。

将此添加到所有方法:

NSLog(@"%s", __PRETTY_FUNCTION__);

这将准确记录每一步发生的事情。

关于ios - Objective-C:在子类中使用继承和调用方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21265157/

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