为什么我的UIButton不响应触摸?

问题描述:

我敢肯定,我忽视了显而易见的,因为我有无数的工作按钮......但...无论出于何种原因,这一个不合作...为什么我的UIButton不响应触摸?

我已经添加了一个UIButton(圆角Rect)到UIView子类(DialogView),它是我的视图控制器视图的子视图。这个子视图几乎完全在IB中创建。我已将IB的(IBAction)okButtonPressed:(id)sender按钮连接到TouchUp Inside,并在DialogView中创建了相应的方法。但是,当我“触摸”这个按钮时,它不会触发该方法。 userInteractionEnabled is true for the VC's view,DialogViewand 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 
+0

'Dialog'是否在'MyViewController'上查看保留属性? – 2009-11-13 22:04:01

+0

修正,应该读“是'DialogView'上'MyViewController'一个保留的财产? – 2009-11-13 22:04:41

+0

哦,什么边界时'self.DialogView'报告后,您将其'center'财产? – 2009-11-13 22:07:04

我认为我们应该使用-setTitle:forState:为了设置按钮的标题?

另一个想法,你检查了按钮的框架不是CGRectZero?顺便说一下,层次结构中所有视图的框架?并检查层次结构中的一个超级视图是否禁用了用户交互? 而且,我认为imageView对触摸没有反应,你的代码中有一个吗?

+2

imageView的东西很棘手如果你添加一个UIButton作为子视图的UIImageView,无论您设置了多少次userInteractionEnabled,您的按钮都将被禁用。 – 2012-10-04 22:23:24

你也许会对彼此顶部两个按钮?将IB项目窗口更改为详细视图,并查看您的视图是否具有比您期望的按钮更多的按钮。也许你已经连接了一个并没有真正获得你所期望的新闻的按钮。

+0

我详细查看...和nope。只有一个UIButton。3 UILabels ...但它们不相关(并且正常工作) – Meltemi 2009-11-13 19:55:01

+0

您提到“几乎完全创建在IB“。你能详细说明一下吗?什么部分不是在IB? – 2009-11-13 20:19:36

+0

好的。我发布了所有与这个烦人的按钮相关的代码 – Meltemi 2009-11-13 21:34:49

我只是有或多或少相同的问题,我发现我包含的视图没有“用户交互启用”。

希望这会有所帮助。