代码之家  ›  专栏  ›  技术社区  ›  Rudiger

选择器已发送到父级而不是自身

  •  1
  • Rudiger  · 技术社区  · 14 年前

    [button addTarget:self action:@selector(method:)...
    

    编辑:和超级使应用程序崩溃

    为任何帮助干杯。

    2 回复  |  直到 14 年前
        1
  •  0
  •   RickiG    14 年前

    按照我理解你的问题的方式,你正在尝试实现一些与委托模式完全相同的东西,但是按照你处理它的方式,你将不会得到任何编译器提示或错误如果超类没有为目标实现正确的方法,它将在你运行它时崩溃。

    就封装而言,这似乎是一个好主意,但我认为如果您在其他地方重用视图组件,则很难进行调试。

    我使用的尝试传递“self”以外的消息的方法的唯一情况是,ViewController有X个视图,这些视图彼此需要,并且需要对彼此的操作做出反应。尽管如此,如果我实现了一个viewController作为视图的委托,我只会让viewController将其他视图更改为它们应该进入的任何状态。

    (希望我没有误解这个问题)

        2
  •  7
  •   imaginaryboy    14 年前

    @interface Base {
    }
    - (IBAction)method:(id)sender;
    @end
    
    @interface Derived {
    }
    @end
    

    如果派生的没有自己的实现 method: 然后您的标准设置 self @selector(method:) 我会做你想做的。

    方法: 然后您别无选择,只能在派生类中添加自己的方法,而派生类只会将调用转发给超类实现。

    - (void)callMethodOnSuper:(id)sender {
        [super method:sender];
    }
    

    然后使用 @selector(callMethodOnSuper:) super 作为目标。

    为了确保我说清楚,我会重申你的情况。您有一个视图控制器,比如MyViewContoller,它的视图有三个子视图。其中一个子视图MyCustomView有一些UIButton子视图。

    我的建议如下:

    @interface MyViewController {
    }
    @end
    
    @interface MyCustomView {
        UIButton* button1;
        UIButton* button2;
    }
    @property (nonatomic,readonly,retain) UIButton* button1;
    @property (nonatomic,readonly,retain) UIButton* button2;
    @end
    

    [myCustomView.button1 addTarget:self action:@selector(method:)...
    [myCustomView.button1 addTarget:self action:@selector(method:)...