Why isn't my UIButton responding to touches?

为君一笑 提交于 2020-01-13 04:48:13

问题


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

回答1:


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 ?




回答2:


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:


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.



来源:https://stackoverflow.com/questions/1731377/why-isnt-my-uibutton-responding-to-touches

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!