Why isn't my UIButton responding to touches?

2019-04-09 18:44发布

I'm sure I'm overlooking the obvious as I've got countless working buttons...but...for whatever reason this one is not cooperating...

I've added a UIButton (Rounded Rect) to a UIView subclass (DialogView) which is a subview of my view controller's view. This subview is created almost entirely in IB. I've wired up the button to (IBAction)okButtonPressed:(id)sender in IB to Touch Up Inside and created a corresponding method in DialogView. However when I "touch" this button it doesn't trigger the method. userInteractionEnabled is true for the VC's view, DialogView and the UIButton.

Thinking maybe initWithCoder had to do some frame manipulation or something I added the following which successfully logs to console.

- (id)initWithCoder:(NSCoder *)decoder {
    if (self = [super initWithCoder:decoder]) {
        NSLog(@"DialogView initWithCoder called");
    }
    return self;
}

In further exploration I wired up an IBOutlet to the button and then if I try to change the titleLabel from the view controller I notice that it get's severely truncated. Default text of say "Press Me!" set in IB displays fine when view is first drawn. But if I change the text...

self.DialogView.okButton.titleLabel.text = @"Not Working";

...it gets truncated to "N..."

Dunno if this is related. Probably...

Anyone see what I've screwed up here?

Edit (adding code related to showing UIButton):

From the View Controller:

self.DialogView = [[[NSBundle mainBundle] loadNibNamed:@"DialogView" owner:self options:nil] objectAtIndex:0];; 
self.DialogView.myVC = self;
self.DialogView.backgroundColor = [UIColor clearColor];
self.DialogView.center = CGPointMake(self.view.frame.size.width / 2, self.view.frame.size.height / 2);
self.DialogView.nameLabel.text = loan.fullName;
self.DialogView.noteLabel.text = loan.summaryOfLoan;
self.DialogView.amountLabel.text = [currencyFormatter stringFromNumber:loan.originalAmount];
self.DialogView.alpha = 0.0;
[self.view addSubview:DialogView];

The UILabels all displaying as expected. As is the problem UIButton. I can see it I just can't interact with it!?!

DialogView's interface:

@class MyViewController;

@interface DialogView : UIView {
    IBOutlet UILabel *nameLabel, *noteLabel, *amountLabel;
    IBOutlet UIImageView *arrowView;
    IBOutlet UIButton *okButton;
    MyViewController *myVC;
}

@property (nonatomic, retain) UILabel *nameLabel, *noteLabel, *amountLabel;
@property (nonatomic, retain) UIImageView *arrowView;
@property (nonatomic, assign) MyViewController *myVC;
@property (nonatomic, retain) UIButton *okButton;

- (IBAction)okButtonPressed:(id)sender;

@end

And DialogView's implementation:

#import "DialogView.h"
#import "MyViewController.h"

@implementation DialogView

@synthesize nameLabel, noteLabel, amountLabel, arrowView, okButton;
@synthesize myVC;

- (void)dealloc {
    [nameLabel release];
    [noteLabel release];
    [amountLabel release];
    [arrowView release];
    [okButton release];
    [super dealloc];
}

- (id)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        // Initialization code
    }
    return self;
}

- (id)initWithCoder:(NSCoder *)decoder {
    if (self = [super initWithCoder:decoder]) {
        NSLog(@"DialogView initWithCoder called");
    }
    return self;
}

- (IBAction)okButtonPressed:(id)sender {
    NSLog(@"pressed DialogView OK button");
    [self.myVC.navigationController popViewControllerAnimated:YES];
}

- (void)drawRect:(CGRect)rect {
    // Drawing code
}


@end

3条回答
Evening l夕情丶
2楼-- · 2019-04-09 19:15

I was just having more or less the same problem and I found that my containing view did not have "User Interaction Enabled".

Hope this helps.

查看更多
祖国的老花朵
3楼-- · 2019-04-09 19:18

Do you maybe have two buttons on top of one another? Change the IB project window to the detail view and see if your view has more buttons than you are expecting. Maybe you've wired up a button that's not actually getting the press you're expecting.

查看更多
唯我独甜
4楼-- · 2019-04-09 19:20

I thought that we should use -setTitle:forState: in order to set button's title ?

An other thought, did you check that the button's frame is not CGRectZero ? And by the way, all the frames for the view in the hierarchy ? And check that one superview in the hierarchy is not user interaction disabled ? And, I think imageView does not respond to touches, do you have one in your code ?

查看更多
登录 后发表回答