ไม่ได้เรียกฟังก์ชัน 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");   
}

ปุ่มถูกวาดบนหน้าจอ แต่เมื่อฉันพยายามแตะ ปุ่มกลับไม่เข้าสู่ฟังก์ชันการกดปุ่ม (ไม่ใช่ NSLog)

มีข้อเสนอแนะอะไรบ้าง? นอกจากนี้ฉันยังไม่เข้าใจว่า addTarget หมายถึงอะไร (ฉันได้อ่านเอกสารของ Apple แล้ว)

อัปเดต 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>>

และ:

(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 ที่แสดงสิ่งแปลกใหม่ให้ฉันเห็น


person Bianca Ioana    schedule 11.01.2015    source แหล่งที่มา
comment
ตัวเลือก user Interaction Enabled ปิดอยู่สำหรับการเลื่อนใช่หรือไม่ ฉันลองใช้รหัสของคุณแล้วและมันก็ใช้งานได้   -  person gabbler    schedule 11.01.2015
comment
คำแนะนำ 1: ค้นหามุมมองใดๆ ของปุ่มโดยตั้งค่า userInteractionEnabled เป็น NO   -  person Jeffery Thomas    schedule 11.01.2015
comment
คำแนะนำที่ 2: เป็นการทดสอบ ให้ลบตัวจดจำท่าทางทั้งหมดออก พวกเขาสามารถรบกวนกิจกรรมการสัมผัสได้   -  person Jeffery Thomas    schedule 11.01.2015
comment
ตั้งค่า buttonHolderView.userInteractionEnabled = true;   -  person Muhammad Adnan    schedule 11.01.2015
comment
ใช่ในรหัสของฉันเปิดอยู่: [scroll setUserInteractionEnabled:YES]; และไม่ทำงาน..   -  person Bianca Ioana    schedule 11.01.2015
comment
แต่เป้าหมายนั้นถูกต้องหรือไม่?   -  person Bianca Ioana    schedule 11.01.2015
comment
ฉันได้เปิดใช้งานการโต้ตอบสำหรับ buttonHolderView แล้ว แต่ก็ยังไม่มีอะไรเลย   -  person Bianca Ioana    schedule 11.01.2015


คำตอบ (2)


รหัสของคุณสำหรับปุ่มนั้นดูดี ยกเว้น xPossition บิต yPossition จะถูกคอมไพล์อย่างดีและ buttonPressed: กำลังถูกเรียก ฉันสงสัยว่าจะมีปัญหากับลำดับชั้นการดูของคุณตามที่แนะนำไปแล้วในความคิดเห็นก่อนหน้านี้ ฉันจะตั้งค่าเบรกพอยต์ที่ส่วนท้ายของเมธอด `viewDidLoad: จากนั้นในช่วงพักคอนโซลให้พิมพ์คำสั่งต่อไปนี้และตรวจสอบว่าปุ่มของคุณซ้อนทับหรือไม่

po [[UIWindow keyWindow] recursiveDescription]

คุณอาจต้องการคัดลอกและวางผลลัพธ์ลงในโปรแกรมแก้ไขข้อความเพื่อให้อ่านง่ายขึ้น

หวังว่าจะช่วยได้! ป้อนคำอธิบายรูปภาพที่นี่

person DoertyDoerk    schedule 11.01.2015
comment
ฉันได้เพิ่มภาพหน้าจอลงในคำตอบก่อนหน้าของฉัน เมื่อแอปถึงจุดพัก ให้คัดลอกและวางคำสั่งที่ฉันชี้ให้คุณเห็นในหน้าต่างคอนโซลที่ด้านล่างขวาของหน้าจอ Xcode ดังที่แสดงด้านบนแล้วกด return คุณอาจต้องการลองทำลายที่ส่วนท้ายของ -viewDidAppear หากผลลัพธ์ยังไม่สามารถสรุปได้ - person DoertyDoerk; 12.01.2015
comment
พอเขียนคำสั่งกลับเป็นศูนย์ในแถวถัดไป ทำอย่างไร? - person Bianca Ioana; 12.01.2015
comment
ฉันเพิ่ง Google มันและดูเหมือนว่าจะเป็นทางเลือกแทนลำดับชั้นการดู Debug ฉันจะลองทั้งสองอย่าง ฉันหว่านบางอย่างเกี่ยวกับเรื่องนี้เมื่อสองสามเดือนก่อน แต่ฉันไม่รู้ว่ามันคืออะไรและจะใช้มันอย่างไร ขอบคุณสำหรับจุดเริ่มต้น - person Bianca Ioana; 12.01.2015
comment
ฉันได้เรียกใช้: (lldb) po [scroll recursiveDescription] - person Bianca Ioana; 12.01.2015
comment
ตรวจสอบร่องรอยที่สองของคุณ บรรทัดที่ 2 ซึ่งให้รายละเอียดเกี่ยวกับภาพพื้นหลัง ในที่นี้จะระบุว่า userInteractionEnabled = NO ฉันจะตั้งค่านี้เป็น YES ใน -viewDidLoador หรือกำจัดมันทั้งหมดอย่างน้อยก็เพื่อการทดสอบ - person DoertyDoerk; 12.01.2015
comment
ลบภาพพื้นหลังของปุ่มออก แต่ก็ยังไม่สามารถแตะได้ - person Bianca Ioana; 12.01.2015
comment
คุณได้ตั้งค่า button.userInteractionEnabled = YES; ? - person DoertyDoerk; 12.01.2015
comment
ให้เราสนทนาต่อในการแชท - person DoertyDoerk; 12.01.2015

อาจลองสิ่งนี้:

[buttonHolderView bringSubviewToFront:button];
person weso    schedule 11.01.2015
comment
ดูเหมือนเกือบจะมองเห็นฉัน แต่แตะฉันไม่ได้ ปุ่มนั้นมองเห็นได้ แต่บางทีมันอาจจะอยู่ในกรอบที่เล็กกว่า ตัวอย่าง: เฟรมที่มีความสูง 100 และปุ่มในตำแหน่ง 120 y คุณสามารถมองเห็นได้แต่แตะไม่ได้ - person weso; 12.01.2015