Why is my UIButton not responding to touch?

I am sure that I am losing sight of the obvious, because I have countless working buttons ... but ... for some reason this person is not cooperating ...

I added UIButton (Rounded Rect) to a subclass of UIView (DialogView), which is the view control of my view controller. This similarity is created almost entirely in IB. I connected the (IBAction)okButtonPressed:(id)sender button in IB before Touch Up Inside and created the corresponding method in DialogView. However, when I touch this button, this does not call the method. userInteractionEnabled true to view VC, DialogView and UIButton .

The thought, perhaps, initWithCoder was to do some frame manipulation or something that I added that successfully logs into the console.

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

In further research, I connected the IBOutlet button to the button, and then, if I try to change the Label header from the view controller, I noticed that it is very truncated. The default text β€œ Press Me! ” Set to IB is displayed in the order when the view is first drawn. But if I changed the text ...

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

... it truncates to "N ..."

I do not know if this is connected. Maybe...

Does anyone see what I messed 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]; 

UILabels all display as expected. Like the UIButton problem. I see this, I just can’t interact with him!?!

DialogView 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 the DialogView 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 
+4
source share
3 answers

I thought we should use -setTitle: forState: to set the name of the button?

Another thought, did you check that the button frame is not CGRectZero? And by the way, all the frames to represent in the hierarchy? And check that one supervisor in the hierarchy is not disconnected from user interaction? And, I think, imageView does not respond to touches, do you have one in the code?

+8
source

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

Hope this helps.

+3
source

Perhaps you have two buttons on top of each other? Change the IB project window to a detailed view and see if there are more buttons in your view than you expect. You may have connected a button that does not actually receive the press that you expect.

0
source

All Articles