为什么我的UIButton没有响应接触?

Mel*_*emi 4 cocoa-touch uibutton uiviewcontroller uiview

我确信我忽略了显而易见的事情,因为我有无数的工作按钮......但是......无论出于什么原因,这个都没有合作......

我已经UIButtonUIView子类(DialogView)中添加了一个(Rounded Rect),它是我的视图控制器视图的子视图.此子视图几乎完全在IB中创建.我已将(IBAction)okButtonPressed:(id)senderIB中的按钮连接到Touch Up Inside并在DialogView中创建了相应的方法.但是,当我"触摸"此按钮时,它不会触发该方法.userInteractionEnabled真正的VC的看法,DialogView 以及UIButton.

思考可能initWithCoder不得不做一些帧操作或我添加以下成功登录到控制台的东西.

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

在进一步的探索中,我连接IBOutlet到按钮,然后如果我尝试从视图控制器更改titleLabel,我注意到它被严重截断.首次绘制视图时,在IB中设置的默认文本" Press Me! "显示正常.但如果我改变文字......

self.DialogView.okButton.titleLabel.text = @"Not Working";
Run Code Online (Sandbox Code Playgroud)

......它被截断为"N ......"

如果这是相关的Dunno.大概...

有谁看到我搞砸了什么?

编辑(添加与显示相关的代码UIButton):

从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];
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

和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
Run Code Online (Sandbox Code Playgroud)

Unf*_*ter 8

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

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

  • 那个imageView的东西很棘手.如果您将UIButton添加为UIImageView的子视图,则无论您设置userInteractionEnabled多少次,您的按钮都将被禁用. (2认同)