不调用UIButton函数

时间:2015-01-11 14:41:45

标签: ios objective-c uibutton

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

所以我正在做的是: 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 [scroll recursiveDescription]。 它回来了:

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>>

AND:

(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向我展示了一些全新的内容。

2 个答案:

答案 0 :(得分:2)

按钮本身的代码看起来很好。除了xPossition之外,yPossition对它进行了很好的编译,并且正在调用buttonPressed:。我怀疑您的视图层次结构存在问题,如前面的评论中已经提到的那样。我在`viewDidLoad:方法的末尾设置了一个断点。然后在控制台中断时输入以下命令,并检查按钮是否重叠。

po [[UIWindow keyWindow] recursiveDescription]

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

希望有所帮助! enter image description here

答案 1 :(得分:1)

也许试试这个:

[buttonHolderView bringSubviewToFront:button];