gpt4 book ai didi

iphone - 制作自定义类的按钮触发父类

转载 作者:行者123 更新时间:2023-12-01 19:22:44 25 4
gpt4 key购买 nike

我有一个 UIViewController称为“MainViewController”,它使用自定义类“CustomController”来控制其中一个 subview 。自定义类实例化 UIButton s 在代码中。如何让这些按钮触发 MainViewController 中的方法?

最佳答案

我认为代表团是去那里的方式。

  • 定义协议(protocol)CustomControllerDelegate里面 CustomController.h使用这样的方法,例如:
    - (void) customControllerButtonPressed(id)sender; // BTW: you can use `CustomController` instead of `id` if you make a forward declaration for this class
  • 添加一个委托(delegate)属性并在.m中合成它文件
    @property (assign) id<CustomControllerDelgate> delegate;
  • 现在,当您按下按钮时,您只需调用委托(delegate):
    [self.delegate customControllerButtonPressed:self];
  • 在您的 MainViewController您使其符合指定的协议(protocol)并像这样设置 CustomControllers 委托(delegate):
    CustomViewController *customVC = [[CustomViewController alloc] init];
    customVC.delegate = self;
  • 现在按下按钮时,MainViewController的指定方法的实现被调用。
  • 关于iphone - 制作自定义类的按钮触发父类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9354517/

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