简体   繁体   English

我的子视图中的UIButton无法正常工作

[英]A UIButton in my subview won't work

I'm Creating a custom UIView called InfoAboutBlockView, I'm adding it to my ViewController and it added correctly but when I'm pressing a button inside that custom UIView it won't fire. 我正在创建一个名为InfoAboutBlockView的自定义UIView,将其添加到ViewController中,并且已正确添加,但是当我按下该自定义UIView中的按钮时,它将不会触发。

I'm creating a xib file in which I design the UIView and then I create the .h and .m files 我正在创建一个xib文件,在其中设计UIView,然后创建.h和.m文件

InfoAboutBlockView.h: InfoAboutBlockView.h:

#import <UIKit/UIKit.h>

@interface InfoAboutBlockView : UIView

- (instancetype)init2;

@property (strong, nonatomic) IBOutlet UIView *contentView;

@end

the contentView is the UIView linked to the .h file. contentView是链接到.h文件的UIView。

InfoAboutBlockView.m InfoAboutBlockView.m

#import "InfoAboutBlockView.h"

@implementation InfoAboutBlockView


- (instancetype)init2 {
    self = [super init];
    if (self) {
        [self loadViewsFromBundle];
    }
    return self;
}
- (void)loadViewsFromBundle {
    NSString *class_name = NSStringFromClass([self class]);
    [[NSBundle mainBundle] loadNibNamed:class_name owner:self options:nil];
    [self addSubview:self.contentView];
}
- (IBAction)startBlockButtonPressed:(id)sender {
    //This Won't Fire
}

@end

The startBlockButtonPressed is the button connected to the .m file. startBlockButtonPressed是连接到.m文件的按钮。 I set a breakpoint at startBlockButtonPressed but it never fires. 我在startBlockButtonPressed处设置了一个断点,但它从未触发。

This is how I add the custom subview to my ViewController: 这是将自定义子视图添加到ViewController的方法:

InfoAboutBlockView *infoAboutBlockView = [[InfoAboutBlockView alloc]init2];
[self.view addSubview:infoAboutBlockView];

I tried bringing it to the front using [self.view bringSubviewToFront:infoAboutBlockView]; 我尝试使用[self.view bringSubviewToFront:infoAboutBlockView];将其带到最前面[self.view bringSubviewToFront:infoAboutBlockView]; and checked that it isn't nil and that the button isn't nil but it just won't fire. 并检查它不是nil并且按钮不是nil但不会触发。

This is basically the same answer I gave here: https://stackoverflow.com/a/22188740/341994 这基本上是我在这里给出的相同答案: https : //stackoverflow.com/a/22188740/341994

The problem is that your InfoAboutBlockView has no size. 问题是您的InfoAboutBlockView没有大小。 (You never gave it a frame, so its frame is zero.) Therefore it is not touchable. (您从未给过它一个框架,因此它的框架为零。)因此,它是不可触摸的。 Therefore none of its subviews are touchable. 因此,其所有子视图均不可触摸。 The button is visible, and the InfoAboutBlockView's contentView may be visible, but they are outside the bounds of the InfoAboutBlockView, which is the size of a single point. 该按钮是可见的,并且InfoAboutBlockView的contentView可能是可见的,但它们超出了InfoAboutBlockView的范围,后者是单个点的大小。 And a subview outside its superview's bounds cannot be touched. 而且,无法触及其超级视图范围之外的子视图。

This would be more evident if you gave the InfoAboutBlockView a weird background color. 如果为InfoAboutBlockView提供了怪异的背景色,这将更加明显。 You won't see that color when it loads. 加载时您不会看到该颜色。

InfoAboutBlockView *infoAboutBlockView = [[InfoAboutBlockView alloc]init2];
[self.view addSubview:infoAboutBlockView];
infoAboutBlockView.backgroundColor = [UIColor redColor]; // you won't see the red

Another way to see this would be set the InfoAboutBlockView's clipsToBounds to YES; 另一种查看方式是将InfoAboutBlockView的clipsToBounds设置为YES。 in that case, you wouldn't see the button. 在这种情况下,您将看不到该按钮。

InfoAboutBlockView *infoAboutBlockView = [[InfoAboutBlockView alloc]init2];
[self.view addSubview:infoAboutBlockView];
infoAboutBlockView.clipsToBounds = YES; // you won't see the button!

Basically that is the whole cause of the trouble; 基本上,这就是麻烦的全部原因; you are being fooled by the fact that when clipsToBounds is NO, subviews outside the bounds of a view are visible (that is what clipsToBounds is about). clipsToBounds为NO时,可以看到视图范围之外的子视图(这就是clipsToBounds ),这一事实使您clipsToBounds Visible, but (because of the way touch works on iOS) not touchable. 可见,但是(由于iOS上的触摸方式)不可触摸。

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

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