【问题标题】:Button on iOS is not sending events on touchUpInsideiOS 上的按钮未在 touchUpInside 上发送事件
【发布时间】:2012-07-18 18:23:15
【问题描述】:

我从我的自定义视图中创建了一个 .xib 文件。 我为该视图创建了 .h/.m 文件。 我 ctrl 从按钮拖动到头文件以创建 IBAction 并将值设置为 touchUpInside。这是正在发生的事情:

http://screencast.com/t/R1WTpK7xp

WTF?

up在按钮外触发事件?

编辑:

截图如下:

反对票是怎么回事?我看不出这有什么意义。

查看.h

#import <UIKit/UIKit.h>
#import "DrawingViewDelegate.h"

@interface DrawingBottomToolbarView : UIView

@property (weak) id <DrawingViewDelegate> delegate;

- (IBAction)lineSegmentButtonPush:(id)sender;

@end

查看.m

#import "DrawingBottomToolbarView.h"

@implementation DrawingBottomToolbarView

@synthesize delegate;

- (id)initWithFrame:(CGRect)frame
{
    NSLog(@"frame");
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        [self addSubview:[[[NSBundle mainBundle] loadNibNamed:@"DrawingBottomToolbarView" owner:self options:nil] objectAtIndex:0]];
        //[[[NSBundle mainBundle] loadNibNamed:@"DrawingBottomToolbarView" owner:self options:nil] objectAtIndex:0];
        //[self addSubview:self.];
    }

    return self;
}

//-(id)initWithCoder:(NSCoder *)aDecoder{
//    
//    NSLog(@"coder");
//    if ((self = [super initWithCoder:aDecoder])){
//        [self addSubview:[[[NSBundle mainBundle] loadNibNamed:@"DrawingBottomToolbarView" owner:self options:nil] objectAtIndex:0]];
//    }
//    return self;
//}

- (IBAction)lineSegmentButtonPush:(id)sender 
{

     NSLog(@"line push");
}

@end

我不明白问题出在哪里。

编辑 2:

我尝试将按钮设置为插座并在代码中添加目标/操作,同样的事情发生了:

.h

@property (weak, nonatomic) IBOutlet UIButton *lineSegmentButton;

.m

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        [self addSubview:[[[NSBundle mainBundle] loadNibNamed:@"DrawingBottomToolbarView" owner:self options:nil] objectAtIndex:0]];
        self.currentSelectedPathSegment = NoneSegment;

        [self.lineSegmentButton addTarget:self action:@selector(lineSegmentButtonPush:) forControlEvents:UIControlEventTouchUpInside];

    }

    return self;
}

编辑 3: 这是我添加两个视图的地方。绘图视图是在代码中创建的,bottomToolbar 是从 .xib 文件中创建的。

kBottomToolbarHeight 是常数,其值与 .xib 文件中定义的高度相同。

- (void)viewWillAppear:(BOOL)animated
{
    [self.view addSubview:self.drawingView];
    [self.view addSubview:self.bottomToolbar];

    CGRect selfRect = self.view.bounds;
    CGRect drawingViewRect = selfRect;
    CGRect bottomToobarRect = selfRect;

    drawingViewRect.size.height = selfRect.size.height - kBottomToolbarHeight;
    bottomToobarRect.size.height = kBottomToolbarHeight;
    bottomToobarRect.origin.y = drawingViewRect.size.height;

    self.drawingView.frame = drawingViewRect;    
    self.bottomToolbar.frame = bottomToobarRect;    
}

【问题讨论】:

  • 这可能意味着您连接错误的操作,您有按钮连接检查器的屏幕截图吗?
  • 嘿,我用截图编辑了问题。如您所见,它设置在 Touch Up Inside 上。
  • 如果在代码中添加动作会发生什么?
  • 尝试删除按钮/插座等,清理项目并从头开始(即再次制作按钮/连接)
  • 尽管如此,设置一个插座以访问该按钮。然后,在代码中,添加目标/动作(如@Dan 建议的那样)并查看它是否有效......

标签: ios uibutton touch xib


【解决方案1】:

您在截屏视频中提到和显示的行为与我在视图层次结构中的某个位置有一个带有 UITapGestureRecognizer 的父视图时所经历的完全相同。

我不确定是否将您的问题标记为可能与 this one which helped me solve my problem 重复。

作为参考,这在 iOS 6.0 中不是问题,只是更早的版本。

【讨论】:

  • 当我将 Interface Builder (Storyboard) 和代码结合起来时,就会发生这种情况。在我在代码中定义了所有内容之后,我让一切都重新开始工作了。你用UITapGestureRecognizer 提到的事情可能只是原因,因为我也有它。我会接受你的回答,因为很可能是那个识别器造成的。
猜你喜欢
  • 2018-01-12
  • 2019-06-06
  • 1970-01-01
  • 1970-01-01
  • 2013-05-11
  • 2014-06-25
  • 1970-01-01
  • 2014-01-14
  • 1970-01-01
相关资源
最近更新 更多