为什么我的UIButton不响应触摸?
我敢肯定,我忽视了显而易见的,因为我有无数的工作按钮......但...无论出于何种原因,这一个不合作...为什么我的UIButton不响应触摸?
我已经添加了一个UIButton
(圆角Rect)到UIView
子类(DialogView),它是我的视图控制器视图的子视图。这个子视图几乎完全在IB中创建。我已将IB的(IBAction)okButtonPressed:(id)sender
按钮连接到TouchUp Inside,并在DialogView中创建了相应的方法。但是,当我“触摸”这个按钮时,它不会触发该方法。 userInteractionEnabled is true for the VC's view,DialogView
and the UIButton
。
也许想着initWithCoder
不得不做一些帧操作或者我添加了以下内容,成功记录到控制台。
- (id)initWithCoder:(NSCoder *)decoder {
if (self = [super initWithCoder:decoder]) {
NSLog(@"DialogView initWithCoder called");
}
return self;
}
在进一步探索我的有线了一个IBOutlet
的按钮,然后如果我尝试了titleLabel从视图控制器改变我注意到,它得到的截短了。在第一次绘制视图时,设置在IB中的默认文本说“新闻我!”显示正常。但是,如果我改变文字...
self.DialogView.okButton.titleLabel.text = @"Not Working";
...它被截断,以 “N ...”
说不上这是否有关。可能...
有人看到我在这里搞砸了吗?
编辑(添加有关表示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
。我可以看到它,我只是不能与它互动!?!
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:为了设置按钮的标题?
另一个想法,你检查了按钮的框架不是CGRectZero?顺便说一下,层次结构中所有视图的框架?并检查层次结构中的一个超级视图是否禁用了用户交互? 而且,我认为imageView对触摸没有反应,你的代码中有一个吗?
imageView的东西很棘手如果你添加一个UIButton作为子视图的UIImageView,无论您设置了多少次userInteractionEnabled,您的按钮都将被禁用。 – 2012-10-04 22:23:24
我只是有或多或少相同的问题,我发现我包含的视图没有“用户交互启用”。
希望这会有所帮助。
'Dialog'是否在'MyViewController'上查看保留属性? – 2009-11-13 22:04:01
修正,应该读“是'DialogView'上'MyViewController'一个保留的财产? – 2009-11-13 22:04:41
哦,什么边界时'self.DialogView'报告后,您将其'center'财产? – 2009-11-13 22:07:04