首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UIButton addTarget不工作

UIButton addTarget不工作
EN

Stack Overflow用户
提问于 2013-02-26 00:29:15
回答 4查看 12.9K关注 0票数 4

ViewController:

代码语言:javascript
复制
@implementation PaiLifeViewController
@synthesize detail = _detail;
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    UIImage *i = [UIImage imageNamed:@"menu_patcode_normal"];
    UIButton *c1 = [[UIButton alloc] init];
    c1.tag = 11;
    UIButton *c2 = [[UIButton alloc] init];
    c2.tag = 12;
    UIButton *c3 = [[UIButton alloc] init];
    c3.tag = 13;
    UIButton *c4 = [[UIButton alloc] init];
    c4.tag = 21;
    UIButton *c5 = [[UIButton alloc] init];
    c5.tag = 22;
    UIButton *c6 = [[UIButton alloc] init];
    c6.tag = 23;
    UIButton *c7 = [[UIButton alloc] init];
    c7.tag = 31;
    UIButton *c8 = [[UIButton alloc] init];
    c8.tag = 32;
    UIButton *c9 = [[UIButton alloc] init];
    c9.tag = 33;

    [c1 setBackgroundImage:i forState:UIControlStateNormal];
    [c2 setBackgroundImage:i forState:UIControlStateNormal];
    [c3 setBackgroundImage:i forState:UIControlStateNormal];
    [c4 setBackgroundImage:i forState:UIControlStateNormal];
    [c5 setBackgroundImage:i forState:UIControlStateNormal];
    [c6 setBackgroundImage:i forState:UIControlStateNormal];
    [c7 setBackgroundImage:i forState:UIControlStateNormal];
    [c8 setBackgroundImage:i forState:UIControlStateNormal];
    [c9 setBackgroundImage:i forState:UIControlStateNormal];

    NSArray *c = [[NSArray alloc] initWithObjects:c1,c2,c3,c4,c5,c6,c7,c8,c9, nil];
    _detail= [[PaiLifePageDetail alloc] initWithDataSource:c pageIndex:1];
    for (UIButton *b in c)
    {
        [b addTarget:self action:@selector(clicked:) forControlEvents:UIControlEventTouchUpInside];
    }
    [self.view addSubview:_detail];
}

- (void) clicked : (UIButton *) button
{
    NSLog(@"button = %i", button.tag);
}

@end

自定义视图:

代码语言:javascript
复制
@implementation PaiLifePageDetail
#define MARGINTOTOP 120.0
#define MARGINTOLEFT 20.0
#define WIDTH 80.0
#define HEIGHT 60.0

- (id) initWithDataSource : (NSArray *) dataSource pageIndex : (int) pageIndex;
{
    self = [super init];
    if (self)
    {
        if (dataSource)
        {
            for (UIButton *button in dataSource)
            {
                //page 1 line 1
                if (pageIndex == 1)
                {
                    if (button.tag == 11)
                    {
                        button.frame = CGRectMake(MARGINTOLEFT, MARGINTOTOP, WIDTH * 2 + 20, HEIGHT);
                        [self addSubview:button];
                    }
                    else if (button.tag == 12)
                    {
                        button.frame = CGRectMake(MARGINTOLEFT + WIDTH * 2 + 20 * 2, MARGINTOTOP, WIDTH, HEIGHT);
                        [self addSubview:button];
                    }
                }
                //not page 1 line 1
                else
                {
                    if ( button.tag == 11 )
                    {
                        button.frame = CGRectMake(MARGINTOLEFT, MARGINTOTOP, WIDTH, HEIGHT);
                        [self addSubview:button];
                    }
                    else if ( button.tag == 12 )
                    {
                        button.frame = CGRectMake(MARGINTOLEFT + WIDTH + 20, MARGINTOTOP, WIDTH, HEIGHT);
                        [self addSubview:button];
                    }
                    else if ( button.tag == 13 )
                    {
                        button.frame = CGRectMake(MARGINTOLEFT + (WIDTH + 20) * 2, MARGINTOTOP, WIDTH, HEIGHT);
                        [self addSubview:button];
                    }
                }

                // line 2
                if ( button.tag == 21 )
                {
                    button.frame = CGRectMake(MARGINTOLEFT, MARGINTOTOP + HEIGHT + 20, WIDTH, HEIGHT);
                    [self addSubview:button];
                }
                else if ( button.tag == 22 )
                {
                    button.frame = CGRectMake(MARGINTOLEFT + WIDTH + 20, MARGINTOTOP + HEIGHT + 20, WIDTH, HEIGHT);
                    [self addSubview:button];
                }
                else if ( button.tag == 23 )
                {
                    button.frame = CGRectMake(MARGINTOLEFT + (WIDTH + 20) * 2, MARGINTOTOP + HEIGHT + 20, WIDTH, HEIGHT);
                    [self addSubview:button];
                }

                // line 3
                if ( button.tag == 31 )
                {
                    button.frame = CGRectMake(MARGINTOLEFT, MARGINTOTOP + (HEIGHT + 20) * 2, WIDTH, HEIGHT);
                    [self addSubview:button];
                }
                else if ( button.tag == 32 )
                {
                    button.frame = CGRectMake(MARGINTOLEFT + WIDTH + 20, MARGINTOTOP + (HEIGHT + 20) * 2, WIDTH, HEIGHT);
                    [self addSubview:button];
                }
                else if ( button.tag == 33 )
                {
                    button.frame = CGRectMake(MARGINTOLEFT + (WIDTH + 20) * 2, MARGINTOTOP + (HEIGHT + 20) * 2, WIDTH, HEIGHT);
                    [self addSubview:button];
                }
            }
        }
    }
    return self;
}

@end

但当我点击这些按钮时,它们根本不起作用,请帮助我,非常感谢。

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2013-02-26 00:43:38

这可能是一个触摸处理事件问题。也就是说,触摸永远不会到达你的自定义按钮。要调试此命令,请执行以下操作:

  1. 什么是您的按钮的subclass?
  2. Check userInterction enabled?
  3. 检查按钮的超级视图中是否有UIGestureRecognizer -它们可能会干扰触摸处理。
  4. Is clipsToBounds property NO?(你是不是财产号?)如果是,则view?
  5. Override边界之外的触摸是否为自定义按钮中的touchesBegan,以查看它是否被调用。
票数 10
EN

Stack Overflow用户

发布于 2014-06-04 13:39:02

当我已经在我的按钮的superview下面有按钮时,我也遇到了同样的问题,我想为它添加目标。我改变了按钮的框架,这样问题就解决了。

票数 1
EN

Stack Overflow用户

发布于 2013-02-26 00:38:41

方法:(id)initWithFrame:(CGRect)frame不用于创建那些PaiLifeCustomButton类型的按钮。

或者在调用init后手动设置帧。我没有仔细看过你的代码,但是一切看起来都很好。设置断点以查看代码是否运行。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15071628

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档