我的子视图中的 UIButton 不起作用 [英] A UIButton in my subview won't work

查看:15
本文介绍了我的子视图中的 UIButton 不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个名为 InfoAboutBlockView 的自定义 UIView,我将它添加到我的 ViewController 中并且它添加正确,但是当我在该自定义 UIView 中按下按钮时它不会触发.

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.

我正在创建一个 xib 文件,我在其中设计 UIView,然后创建 .h 和 .m 文件

I'm creating a xib file in which I design the UIView and then I create the .h and .m files

InfoAboutBlockView.h:

#import <UIKit/UIKit.h>

@interface InfoAboutBlockView : UIView

- (instancetype)init2;

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

@end

contentView 是链接到 .h 文件的 UIView.

the contentView is the UIView linked to the .h file.

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

startBlockButtonPressed 是连接到 .m 文件的按钮.我在 startBlockButtonPressed 处设置了一个断点,但它永远不会触发.

The startBlockButtonPressed is the button connected to the .m file. I set a breakpoint at startBlockButtonPressed but it never fires.

这就是我将自定义子视图添加到我的 ViewController 的方式:

This is how I add the custom subview to my ViewController:

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

我尝试使用 [self.view bringSubviewToFront:infoAboutBlockView]; 将它放在前面,并检查它不是 nil 并且按钮不是 nil 但它不会触发.

I tried bringing it to the front using [self.view bringSubviewToFront:infoAboutBlockView]; and checked that it isn't nil and that the button isn't nil but it just won't fire.

推荐答案

这与我在这里给出的答案基本相同:https:///stackoverflow.com/a/22188740/341994

This is basically the same answer I gave here: https://stackoverflow.com/a/22188740/341994

问题是您的 InfoAboutBlockView 没有大小.(你从来没有给它一个框架,所以它的框架是零.)因此它是不可触摸的.因此,它的所有子视图都不可触摸.按钮是可见的,InfoAboutBlockView 的contentView 可能是可见的,但是它们超出了InfoAboutBlockView 的范围,也就是单个点的大小.并且不能触及超出其父视图范围的子视图.

The problem is that your InfoAboutBlockView has no size. (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. And a subview outside its superview's bounds cannot be touched.

如果你给 InfoAboutBlockView 一个奇怪的背景颜色,这会更明显.加载时您不会看到该颜色.

This would be more evident if you gave the InfoAboutBlockView a weird background color. 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

另一种查看方法是将 InfoAboutBlockView 的 clipsToBounds 设置为 YES;在这种情况下,您将看不到该按钮.

Another way to see this would be set the InfoAboutBlockView's clipsToBounds to 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!

基本上这就是麻烦的全部原因;当 clipsToBounds 为 NO 时,视图边界之外的子视图是可见的(这就是 clipsToBounds 的意义所在),这让您被愚弄了.可见,但(由于 iOS 上的触摸工作方式)不可触摸.

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). Visible, but (because of the way touch works on iOS) not touchable.

这篇关于我的子视图中的 UIButton 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆