Sending message to child from parent

后端 未结 3 1631
一生所求
一生所求 2021-01-21 06:02

I have class Child inherited from class Parent. What I want is to send message to child, which has implementation of that message. So it\'s like calling pure virtual function fr

3条回答
  •  一生所求
    2021-01-21 06:16

    Whether the method is present is checked at runtime. Therefore, even if the compiler warns you, the code could still succeed. But always check if self really defines that method first.

    if ([self respondsToSelector:@selector(onCancel)]) {
      [self onCancel];
    }
    

    If you want to eliminate the warning, use -performSelector:.

    if ([self respondsToSelector:@selector(onCancel)]) {
      [self performSelector:@selector(onCancel)];
    }
    

提交回复
热议问题