【问题标题】:UIButton function is not called未调用 UIButton 函数
【发布时间】:2015-03-09 09:50:41
【问题描述】:

您好,提前感谢您的支持:)

所以我正在做的是: 1.创建滚动视图并将其添加到主视图 2.然后在滚动视图中添加另一个与滚动视图一样大的UIView; 3. 在 UIView 中添加一个以编程方式创建的按钮,我还以编程方式添加:标签、图像、大小。

添加(养育)对象的顺序: UIView->UIScrollView->UIView->UIButton

这是我正在使用的代码:

#import "ViewController.h"

@interface ViewController ()
{
    UIButton *button;
    UIView *buttonHolderView;
}
@end

@implementation ViewController

@synthesize scroll;

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    //scroll view is added using storyboards
    [scroll setScrollEnabled:YES];
    [scroll setContentSize:CGSizeMake(1840, 1752)];
    scroll.bounces = NO;
    [scroll setBackgroundColor:[UIColor blackColor]];
    //UPDATE 4
    //here changed to integer coordinates and now it works
    buttonHolderView = [[UIView alloc] initWithFrame:CGRectMake( scroll.frame.origin.x, scroll.frame.origin.y, scroll.frame.size.width, scroll.frame.size.height)];
    [buttonHolderView setBackgroundColor:[UIColor blueColor]];
    [scroll addSubview:buttonHolderView];

    NSString *title = @"";
    int tagForTheButton = 0;

    button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.adjustsImageWhenHighlighted = NO;
    button.frame = CGRectMake(xPossition, yPossition, 200, 64);
    button.tag = tagForTheButton;
    [button setTitle:title forState:UIControlStateNormal];
    [button addTarget:self action:@selector(buttonPressed: ) forControlEvents:UIControlEventTouchUpInside];
    [buttonHolderView addSubview:button];
    [button setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"Tile.png"]] forState:UIControlStateNormal];
    //other scroll and pinch gesture adjustments
    //UPDATE WITH CODE FOR PINCH AND SCROLL 


   [scroll setUserInteractionEnabled:YES];
    UIPinchGestureRecognizer *pitchGestureRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(handlePitch:)];
    [scroll addGestureRecognizer:pitchGestureRecognizer];

    imageMaxScale = 1.1;
    imageMinScale = 0.2;
    imageCurrentScale = 1.0;



    yMyCharacterFrame = 200;
    xMyCharacterFrame = 200;
    myCharacterFrame = [[UIImageView alloc]init];
    myCharacterFrame.frame = CGRectMake(xMyCharacterFrame, yMyCharacterFrame, 100, 100);
    myCharacterFrame.image = [UIImage imageNamed:@"image0.png"];
    [myCharacterFrame setUserInteractionEnabled:YES];
    [scroll addSubview:myCharacterFrame];
}

-(void)handlePitch:(UIPinchGestureRecognizer *) pinchGesture{

    if (imageCurrentScale * [pinchGesture scale] > imageMinScale && imageCurrentScale * [pinchGesture scale] <imageMaxScale) {
        imageCurrentScale = imageCurrentScale * [pinchGesture scale];
        CGAffineTransform zoomTransform = CGAffineTransformMakeScale(imageCurrentScale, imageCurrentScale);
        [[pinchGesture view] setTransform:zoomTransform];
    }


    [pinchGesture setScale:1.0];
    CGRect scrollFrame;
    scrollFrame.origin = _mainViewHolder.frame.origin;
    scrollFrame.size = CGSizeMake(568, 320);
    scroll.frame = scrollFrame;

}

-(void)buttonPressed:(UIButton *)sender{
    NSLog(@"button pressed");   
}

按钮是在屏幕上绘制的,但是当我尝试点击它时,它没有进入 buttonPressed 功能(不是 NSLog)。

有什么建议吗? 另外我不明白 addTarget 指的是什么(我已经阅读了苹果文档)。

更新 2: 如果我将按钮添加到滚动视图中,不仅会显示它,而且我还可以点击它。

更新 3: 我已经运行:po [滚动递归描述]。 它返回:

UIScrollView: 0x7f95c9c7adf0; frame = (0 0; 320 568); clipsToBounds = YES; autoresize = RM+BM; gestureRecognizers = <NSArray: 0x7f95c9c7f5f0>; layer = <CALayer: 0x7f95c9c79820>; contentOffset: {0, 0}; contentSize: {1840, 1752}>
   | <UIImageView: 0x7f95c9c7ed40; frame = (317.5 561; 2.5 7); alpha = 0; opaque = NO; autoresize = LM; userInteractionEnabled = NO; layer = <CALayer: 0x7f95c9c7ec20>>
   | <UIImageView: 0x7f95c9c7d2f0; frame = (313 565.5; 7 2.5); alpha = 0; opaque = NO; autoresize = TM; userInteractionEnabled = NO; layer = <CALayer: 0x7f95c9c7dda0>>
   | <UIButton: 0x7f95c9d9f9c0; frame = (0 0; 320 568); opaque = NO; layer = <CALayer: 0x7f95c9d9e750>>
   |    | <UIButton: 0x7f95c9d9fea0; frame = (0 0; 200 64); opaque = NO; layer = <CALayer: 0x7f95c9d96540>>
|    |    | <UIImageView: 0x7f95c9cc4590; frame = (0 0; 200 64); clipsToBounds = YES; opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x7f95c9cc4690>>

与:

(lldb) po [button recursiveDescription]
<UIButton: 0x7f95cd24e1f0; frame = (0 0; 200 64); opaque = NO; tag = 0; layer = <CALayer: 0x7f95cd24e100>>
   | <UIImageView: 0x7f95cd251b70; frame = (0 0; 200 64); clipsToBounds = YES; opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x7f95cd251c70>>

更新 4: 更改为整数后,代码似乎可以正常工作: buttonHolderView = [[UIView alloc] initWithFrame:CGRectMake(scroll.frame.origin.x, scroll.frame.origin.y, scroll.frame.size.width, scroll.frame.size.height)];

感谢您的所有帮助,尤其是对 DoertyDoerk 的帮助,他向我展示了一些全新的东西。

【问题讨论】:

  • 滚动的user Interaction Enabled 选项已关闭?我尝试了你的代码,它成功了。
  • 建议1:查找按钮的任何superview,userInteractionEnabled 设置为NO
  • 建议2:作为测试,移除所有手势识别器。它们会干扰触摸事件。
  • 设置 buttonHolderView.userInteractionEnabled = true;
  • 是的,在我的代码中它已打开:[scroll setUserInteractionEnabled:YES];并且不工作..

标签: ios objective-c uibutton


【解决方案1】:

按钮本身的代码看起来不错。除了 xPossition,yPossition 位它编译得很好,并且正在调用 buttonPressed:。我怀疑您的视图层次结构存在问题,正如之前的 cmets 中已经建议的那样。我会在你的 `viewDidLoad: 方法的末尾设置一个断点。然后在控制台的中断处键入以下命令并检查您的按钮是否被覆盖。

po [[UIWindow keyWindow] recursiveDescription]

您可能希望将结果复制并粘贴到文本编辑器中以便于阅读。

希望对您有所帮助!

【讨论】:

  • 我在之前的回答中添加了一个屏幕截图。当应用程序遇到断点时,只需将我向您指出的命令复制并粘贴到 Xcode 屏幕右下方的控制台窗口中,如上所示,然后按回车键。如果结果不是决定性的,您也可能想尝试在-viewDidAppear 的末尾进行中断。
  • 当我写命令时它在下一行返回 nil,它应该怎么做?
  • 我只是用谷歌搜索它,它似乎是“调试视图层次结构”的替代品,我会尝试这两个,几个月前我播种了一些关于这个的内容,但我不知道它是什么怎么用,thx为起点
  • 我运行过:(lldb) po [scroll recursiveDescription]
  • 检查您的第二个跟踪,第 2 行,它提供了有关背景图像的详细信息。此处显示 userInteractionEnabled = NO。我会在 -viewDidLoad 中将其设置为 YES,或者至少在测试时完全摆脱它。
【解决方案2】:

不妨试试这个:

[buttonHolderView bringSubviewToFront:button];

【讨论】:

  • 看起来就像“你可以看到我,但你不能触摸我”按钮是可见的,但也许它在一个较小的框架中。示例:高度为 100 的框架和 120 y 位置的按钮,您可以看到它但不能点击它。
猜你喜欢
  • 2015-03-07
  • 1970-01-01
  • 2020-11-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多