-3

I have a button called "buttonPressed". When I press the button, I need to find the x and y co-ordinates to that button. Do you have any ideas that may help me out?

    UIButton *circleButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    circleButton.frame = rect;
    circleButton.layer.cornerRadius = 8.0f;
    [circleButton addTarget:self
                     action:@selector(buttonPressed:)
           forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:circleButton];

-(void)buttonPressed:(id)sender
{

}

Answer

     - (IBAction)buttonPressed:(UIButton *)sender
     {
       CGPoint coordinates = sender.frame.origin;
       CGFloat x = coordinates.x;
       CGFloat y = coordinates.y;
     }
1
  • which coordinates? where do you want to send them?
    – wattson12
    Sep 25, 2012 at 16:23

1 Answer 1

9

Sounds like you're wanting the X & Y off the sender of the action. Attach a sender parameter to your button tap method and find the frame for sender.

- (IBAction)buttonTapped:(UIButton *)sender
{
    CGPoint coordinates = sender.frame.origin;
}
1
  • 1
    Thanks. The problem was: (UIButton *) where I had (id). I didn't saw that :/ Lesson learned
    – Neru-J
    Sep 26, 2012 at 12:00

Not the answer you're looking for? Browse other questions tagged or ask your own question.