本文介绍了为什么我的UIButton不响应触摸?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我相信我会忽略这个明显的,因为我有无数的工作按钮...但是...无论什么原因,这个不合作...I'm sure I'm overlooking the obvious as I've got countless working buttons...but...for whatever reason this one is not cooperating...我已向 UIView 子类(DialogView)中添加了一个 UIButton 我的视图控制器视图的子视图。这个子视图几乎完全在IB中创建。我将IB中的(IBAction)okButtonPressed:(id)sender 连接到 Touch Up Inside ,并在DialogView中创建了相应的方法。然而,当我触摸这个按钮,它不触发的方法。 userInteractionEnabled 为 true 的VC视图, DialogView 和 UIButton 。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.思考也许 initWithCoder 不得不做一些框架操作添加以下成功日志到控制台。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;} 在进一步探索中,我连接了一个 IBOutlet 到按钮,然后如果我尝试更改titleLabel从视图控制器,我注意到它被严重截断。当第一次绘制视图时,在IB中设置的默认文本按我!设置很好。但是如果我改变文本...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"; ...被截断为N ......it gets truncated to "N..." Dunno如果这是相关的。可能...Dunno if this is related. Probably...任何人都看到我在这里搞砸了吗?Anyone see what I've screwed up here?显示 UIButton ):从视图控制器: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都显示为预期。正如问题 UIButton 。The UILabels all displaying as expected. As is the problem UIButton. I can see it I just can't interact with it!?! DialogView的界面:@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 DialogView的实现#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推荐答案我认为我们应该使用-setTitle:forState :为了设置按钮的标题?I thought that we should use -setTitle:forState: in order to set button's title ?另一个想法,你检查按钮的框架不是CGRectZero?顺便说一下,在层次结构中视图的所有帧?并检查层次结构中的一个超级视图是否禁用用户交互? 而且,我认为imageView没有响应touch,你有一个在你的代码?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 ? 这篇关于为什么我的UIButton不响应触摸?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-10 19:43