简体   繁体   English

UIButton目标是UIViewController而不是自定义UIView

[英]UIButton target to be UIViewController not custom UIView

I have a UIViewController which creates a custom sub view. 我有一个UIViewController ,它创建一个自定义子视图。 The sub view is a UIView object which has been subclassed a few times. 子视图是一个UIView对象,该对象已经被子类化了几次。

Within the subview class I create a custom init method: 在子视图类中,我创建了一个自定义的init方法:

-(id)init {
    self = [super init];
    if (self) {
        // Init code
        [self spm_correctGuessViewCustomInit];
    }
    return self;
}

And within this I create a button and a label. 然后在其中创建一个按钮和一个标签。 The question relates directly to the button and its target action. 问题直接与按钮及其目标动作有关。

What I would like is for the UIViewController to have the buttons action, not the subclasses UIView (which actually creates and holds the button). 我想要的是让UIViewController具有按钮操作,而不是UIView的子类(它实际上创建并保存了按钮)。

[continueButton addTarget:self.superview action:@selector(correctGuessContinueButtonPressed) forControlEvents:UIControlEventTouchUpInside];

I pass in the target of self.superview, this appears to work correctly and the correct method is run. 我传入了self.superview的目标,这似乎可以正常工作并且运行了正确的方法。 However, I am shown a warning in the subclass 'Undeclared selector 'correctGuessContinueButtonPressed'' 但是,在子类“未声明的选择器'correctGuessContinueButtonPressed”中显示了警告

So am I implementing this approach correctly? 那我可以正确实现这种方法吗? Please let me know if more information is required. 如果需要更多信息,请告诉我。

Your view.superview approach will not bring you to the view controller, but to a view. 您的view.superview方法不会带您进入视图控制器,而是带您进入视图。

You can import the header of your implementing class to fix the warning, but I think your design should be improved. 您可以导入实现类的标头来修复警告,但是我认为您的设计应该得到改进。 Views should work pretty much on their own and not depend on their superviews, or even worse the whole architecture of views and controllers. 视图应该非常独立地工作,而不是依赖于它们的超级视图,甚至更糟的是视图和控制器的整个体系结构。

I'd pass a delegate down the line that gets called when the user pressed the button, or set some blocks on the views that get called when buttons fire. 我会在用户按下按钮时在行下方传递一个委托,或者在按钮触发时在被调用的视图上设置一些块。

Avoid communication over several layers of abstraction. 避免在多层抽象上进行通信。

One solution would be to update your custom view's init method so it takes target and action parameters (much like the addTarget: method of the button). 一种解决方案是更新自定义视图的init方法,使其采用targetaction参数(非常类似于按钮的addTarget:方法)。 You could then pass these values to the button via the addTarget: call. 然后,您可以通过addTarget:调用将这些值传递给按钮。

- (instancetype)initWithTarget:(id)target action:(SEL)action {
    // your normal init code here
    // use target and action to setup your button
}

Reference previous similar question: Calling a method in a UIViewController from a UIButton in a subview 参考前面类似的问题: 从子视图的UIButton调用UIViewController中的方法

  1. I had to add an import to the View Controller that the method was on, within the custom UIView subclass. 我必须在自定义UIView子类中向该方法所在的View Controller添加一个导入。

  2. With the controller property set I could set the button target as controller 设置好控制器属性后,我可以将按钮目标设置为控制器

  3. Ensure that the method that was being called from the button was in the controller header file, so could be seen by the subview implementation file. 确保从按钮调用的方法在控制器头文件中,因此可以通过子视图实现文件看到。 Previously this was not so the subview was not to know this existed. 以前不是这样,所以子视图不知道它的存在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM