Ios 未调用UIButton函数

Ios 未调用UIButton函数,ios,objective-c,uibutton,Ios,Objective C,Uibutton,您好,提前感谢您的支持:) 所以我现在做的是: 1.创建滚动视图并将其添加到主视图 2.然后在滚动视图中添加另一个与滚动视图一样大的UIView; 3.在UIView中,添加一个以编程方式创建的按钮,我还以编程方式添加:标记、图像、大小 添加(父对象)对象的顺序: UIView->UIScrollView->UIView->UIButton 下面是我正在使用的代码: #import "ViewController.h" @interface ViewController () { UI

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

所以我现在做的是: 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");   
}
#导入“ViewController.h”
@界面视图控制器()
{
UIButton*按钮;
UIView*按钮HolderView;
}
@结束
@实现视图控制器
@综合卷轴;
-(无效)viewDidLoad{
[超级视图下载];
//加载视图后,通常从nib执行任何其他设置。
//使用故事板添加滚动视图
[滚动设置可滚动:是];
[滚动设置contentsize:CGSizeMake(18401752)];
scroll.bounces=否;
[滚动设置背景颜色:[UIColor blackColor]];
//更新4
//这里改为整数坐标,现在可以了
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]];
[滚动添加子视图:按钮HolderView];
NSString*title=@;
int tagForTheButton=0;
button=[UIButton按钮类型:UIButtonTypeCustom];
button.adjustsImageWhenHighlighted=否;
button.frame=CGRectMake(xPosition,yPosition,200,64);
button.tag=tagForTheButton;
[按钮设置标题:状态标题:uicontrol状态正常];
[按钮添加目标:自我操作:@选择器(按钮按下:)用于控制事件:UIControlEventTouchUpInside];
[buttonHolderView添加子视图:按钮];
[按钮setBackgroundImage:[UIImage ImageName:[NSString stringWithFormat:@“Tile.png”]]用于状态:UIControlStateNormal];
//其他滚动和挤压手势调整
//使用压缩和滚动代码更新
[滚动setUserInteractionEnabled:是];
UIPinchGestureRecognizer*pitchGestureRecognizer=[[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(handlePitch:)];
[滚动添加GestureRecognizer:pitchGestureRecognizer];
imageMaxScale=1.1;
imageMinScale=0.2;
imageCurrentScale=1.0;
yMyCharacterFrame=200;
xMyCharacterFrame=200;
myCharacterFrame=[[UIImageView alloc]init];
myCharacterFrame.frame=CGRectMake(xMyCharacterFrame,yMyCharacterFrame,100100);
myCharacterFrame.image=[UIImage ImageName:@“image0.png”];
[myCharacterFrame setUserInteractionEnabled:是];
[滚动添加子视图:myCharacterFrame];
}
-(无效)handlePitch:(UIPinchGestureRecognitor*)pinchGesture{
如果(imageCurrentScale*[pinchGesture scale]>imageMinScale&&imageCurrentScale*[pinchGesture scale]可以尝试以下方法:

[buttonHolderView bringSubviewToFront:button];
也许可以试试这个:

[buttonHolderView bringSubviewToFront:button];
也许可以试试这个:

[buttonHolderView bringSubviewToFront:button];
也许可以试试这个:

[buttonHolderView bringSubviewToFront:button];

按钮本身的代码看起来很好。除了Xposition,Yposition将其编译得很好,正在调用
按钮pressed:
。我怀疑您的视图层次结构有问题,正如前面的评论中所建议的那样。我已经在`viewDidLoad:方法的末尾设置了一个断点。然后在控制台的中断处键入以下命令并检查按钮是否覆盖

po[[UIWindow keyWindow]递归描述]

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

希望有帮助!

按钮本身的代码看起来很好。除了Xposition之外,Yposition对它进行了很好的编译,并且正在调用
按钮:
。我怀疑您的视图层次结构有问题,正如前面的评论中所建议的那样。我已经在`viewDidLoad:方法的末尾设置了一个断点。然后在cons中中断ole键入以下命令并检查按钮是否覆盖

po[[UIWindow keyWindow]递归描述]

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

希望有帮助!

按钮本身的代码看起来很好。除了Xposition之外,Yposition对它进行了很好的编译,并且正在调用
按钮:
。我怀疑您的视图层次结构有问题,正如前面的评论中所建议的那样。我已经在`viewDidLoad:方法的末尾设置了一个断点。然后在cons中中断ole键入以下命令并检查按钮是否覆盖

po[[UIWindow keyWindow]递归描述]

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

希望有帮助!

按钮本身的代码看起来很好。除了Xposition之外,Yposition对它进行了很好的编译,并且正在调用
按钮:
。我怀疑您的视图层次结构有问题,正如前面的评论中所建议的那样。我已经在`viewDidLoad:方法的末尾设置了一个断点。然后在cons中中断ole键入以下命令并检查按钮是否覆盖

po[[UIWindow keyWindow]递归描述]

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

希望有帮助!

滚动条上的
用户交互启用
选项已关闭?我尝试了你的代码,它成功了。建议1:在
userInteractionEnabled
设置为
NO
的情况下,查找按钮的任何超级视图。建议2:作为测试,删除所有手势识别器。它们可能会干扰触摸事件。set buttonHolderView.userInteractionEnabled=true;是在我的代码中它被打开:[scroll setUserInteractionEnabled:Yes];并且不工作。
用户交互启用
选项对于滚动是关闭的?我尝试了你的代码,它工作了。建议1:使用
UserInteractionEnabled查找按钮的任何超级视图