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

查看:90
本文介绍了我的子视图中的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.

subview to my 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.

推荐答案

这基本上是我在这里给出的答案: http://stackoverflow.com/a/22188740/341994

This is basically the same answer I gave here: http://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 是否,视图外边界的子视图是可见的(这是 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天全站免登陆