UIVut中的UIButton没有响应任何事件

时间:2013-07-01 22:42:35

标签: ios5 ios6 uiview uibutton

我正在使用以下代码向UIButton添加UIView

    rejectButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [rejectButton addTarget:self action:@selector(rejectBuddy:) forControlEvents:UIControlEventTouchDown];
    [rejectButton.titleLabel setFont:[UIFont fontWithName:@"Heiti SC Medium" size:15]];
    rejectButton.titleLabel.userInteractionEnabled = NO;
    [rejectButton setTitle:@"Reject" forState:UIControlStateNormal];
    [rejectButton setBackgroundImage:[UIImage imageNamed:@"Logout_Profile.png"] forState:UIControlStateNormal];
    rejectButton.frame = CGRectMake(168, 380, 141.0, 45.0);
    rejectButton.userInteractionEnabled = YES;
    [mainView addSubview:rejectButton];

当我点击按钮时,它没有做任何事情。我甚至没有看到按下按钮的动画。我有什么遗漏吗?

当我在故事板上的UIButton上手动创建UIView时,它正在运行。但对于我的用例,那是不可能的。

2 个答案:

答案 0 :(得分:1)

添加以下内容以启用突出显示。确保您的UIView也启用了userInteractionEnabled。您可能想要使用UIControlEventTouchUp。

rejectButton.showsTouchWhenHighlighted = YES;

并注释掉

//rejectButton.titleLabel.userInteractionEnabled = NO;

答案 1 :(得分:0)

之前遇到此问题,似乎我的动画转换尚未完成。我忘了将completeTransition:Bool放在animateTransition:context方法的末尾。