Cocoa touch 为什么不是';我的UIT按钮对触摸没有反应吗?
我肯定我忽略了显而易见的,因为我有无数的工作按钮…但是…无论出于什么原因,这一个不配合 我在视图控制器视图的子视图Cocoa touch 为什么不是';我的UIT按钮对触摸没有反应吗?,cocoa-touch,uiview,uiviewcontroller,uibutton,Cocoa Touch,Uiview,Uiviewcontroller,Uibutton,我肯定我忽略了显而易见的,因为我有无数的工作按钮…但是…无论出于什么原因,这一个不配合 我在视图控制器视图的子视图UIView子类(DialogView)中添加了UIButton(圆角矩形)。此子视图几乎完全是在IB中创建的。我已将按钮连接到IB中的(iAction)OK按钮按下:(id)发送者,并在DialogView中创建了相应的方法。但是,当我“触摸”这个按钮时,它不会触发该方法userInteractionEnabled对于VC的视图、DialogView和UIButton来说是true
UIView
子类(DialogView)中添加了UIButton
(圆角矩形)。此子视图几乎完全是在IB中创建的。我已将按钮连接到IB中的(iAction)OK按钮按下:(id)发送者,并在DialogView中创建了相应的方法。但是,当我“触摸”这个按钮时,它不会触发该方法userInteractionEnabled对于VC的视图、DialogView
和UIButton
来说是true
我想也许initWithCoder
必须做一些帧处理或者我添加了一些成功登录到控制台的东西
- (id)initWithCoder:(NSCoder *)decoder {
if (self = [super initWithCoder:decoder]) {
NSLog(@"DialogView initWithCoder called");
}
return self;
}
在进一步的探索中,我将一个IBOutlet
连接到按钮,然后如果我试图从视图控制器更改标题标签,我注意到它被严重截断。第一次绘制视图时,IB中设置的“按我!”默认文本显示良好。但是如果我改变文本
self.DialogView.okButton.titleLabel.text = @"Not Working";
…它被截断为“N…”
不知道这是否相关。也许
有人知道我搞砸了什么吗
编辑(添加与显示ui按钮相关的代码)
:
从视图控制器:
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];
UIS标签都按预期显示。正如问题所在ui按钮
。我能看到它,我就是不能和它互动
对话框视图的界面:
@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
#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
和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
#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
你可能有两个按钮在另一个上面吗?将IB项目窗口更改为详细视图,查看视图中的按钮是否比预期的多。也许你连上了一个按钮,但实际上并没有得到你所期望的新闻 我认为我们应该使用-setTitle:forState:来设置按钮的标题
另一个想法是,你是否检查过按钮的边框是否为零?顺便问一下,层次结构中视图的所有帧?并检查层次结构中的一个superview是否未禁用用户交互?
而且,我认为imageView不响应触摸,您的代码中是否有触摸?我只是或多或少遇到了相同的问题,我发现我的包含视图没有“启用用户交互”
希望这有帮助。我正在详细查看。。。没有。只有一个按钮。3个UILabel…但它们是不相关的(并且工作正常)。您提到“几乎完全是在IB中创建的”。你能详细说明一下吗?IB中没有哪个部分?是对话框
查看MyViewController
上的保留属性吗?更正,应为“在MyViewController
上,DialogView
是保留的属性吗?哦,self.DialogView
在设置其center
属性后报告的界限是什么?要更改按钮的文本,您应该始终使用setTitle:forState消息,而不是.title=图像视图的事情很棘手。”。如果将UIButton添加为UIImageView的子视图,则无论将userInteractionEnabled设置多少次,按钮都将被禁用。