LocationInView X & Y Coordinates?

Discussion in 'iOS Programming' started by Darkroom, Jun 3, 2009.

  1. Darkroom Guest

    Darkroom

    Joined:
    Dec 15, 2006
    Location:
    Montréal, Canada
    #1
    i'm trying to print out a string using CGPoint locationInView, but i only receive strange numbers.

    Code:
    UITouch *touch = [touches anyObject];
    CGPoint touchLocation = [touch locationInView:self.view];
    NSString *locationMessage = [[NSString alloc] initWithFormat:@"Location: %f", touchLocation];
    locationLabel.text = locationMessage;
    [locationMessage release];
    
    how can i receive X and Y coordinates as a string?

    locationLable.text (example) of the above code:
    Code:
    Location:  3527480165821054976.000000
    
     
  2. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
    touchLocation is a CGPoint and so won't respond correctly to %***** it in a string format. Try something like this instead:
    Code:
    NSString *locationMessage = [[NSString alloc] initWithFormat:@"Location: x=%f, y=%f", touchLocation.x, touchLocation.y];
    
     
  3. Darkroom thread starter Guest

    Darkroom

    Joined:
    Dec 15, 2006
    Location:
    Montréal, Canada
    #3
    ah! thanks :)... works great.

    at first i tried casting the object as a string but that was crashville and it's suburbs.

    thanks again.

    revised code:
    Code:
    CGPoint touchLocation = [[touches anyObject] locationInView:self.view];
    NSString *locationMessage = [[NSString alloc] initWithFormat:@"Location: X = %.0f Y = %.0f", touchLocation.x, touchLocation.y];
    locationLabel.text = locationMessage;
    [locationMessage release];
    
     
  4. Kpeters213 macrumors newbie

    Joined:
    Apr 25, 2009
    #4
    Thank you so much guys. This really helped me out.

    Showing the before and after code really helped too!

    -KP
     
  5. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #5
    You can also use the functions in UIGeometry.h, like NSStringFromCGPoint()
     

Share This Page