Getting name of the imageview

Discussion in 'iPhone/iPad Programming' started by iphonejudy, Apr 22, 2009.

  1. macrumors 6502

    iphonejudy

    #1
    Is it possible to get the name of the imageview when touch?

    I refered the document.

    and i saw

    Code:
     *   – initWithImage:
    
    Image Data
    
        *   image  property
    
    Animating Images
    
        *   animationImages  property
        *   animationDuration  property
        *   animationRepeatCount  property
        * – startAnimating
        * – stopAnimating
        * – isAnimating
    
    Setting and Getting Attributes
    
        *   userInteractionEnabled  property
    these properties only.
     
  2. Moderator

    robbieduncan

    Staff Member

    #2
    Views don't have names.
     
  3. macrumors 68030

    johnnyjibbs

    #3
    Maybe you could be a bit more specific but remember that UIImageView is a subclass of UIView, which itself is a subclass of UIResponder.

    Therefore, a UIImageView has access to UIResponder's touch methods - i.e. the touchesBegan, touchesMoved, touchesEnded methods.

    As for the name of the image contained within, this is stored as the image object, accessed through the UIImageView's image property.

    So you could say, for example:

    Code:
    // Setting up the image object
    UIImage *anImage = [UIImage imageNamed:@"banana.png"];
    
    // Assigning the image to your image view
    [myImageView setImage:anImage];
    
    You can access the image object at any time by accessing the image view's image property. Something like:

    Code:
    // Get access to the image object inside
    UIImage *theImageInsideTheImageView = myImageView.image;
    
    If you frequently need to refer to certain objects, it may be a good idea to set up an instance variable to easily refer to it by declaring the instance variable in the header file and synthesizing it in the implementation.

    (Note that although I used it for simplicity above, I would strongly advise against the use of UIImage's imageNamed: method as there are some major memory-related bugs in it. You could use initWithContentsOfFile: instead or implement your own image caching if you need to cache the images.)
     
  4. macrumors 6502

    iphonejudy

    #4
    If a view having single imageview , then it is easy to get the image.

    I am having 5 imageviews.containing 5 images.


    I need to find out ,in which imageview the user is touching now?



    Actualy i thought to retrieve the name of the imageview.It is not possible is it?

    Then anyother solution for this?

    Code:
    -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
    {   
    	UITouch *touch = [touches anyObject];
    	NSLog(@"touch=%@",touch);
    .........
    
    }

    I got the below result:


    <UITouch: 0x468850> phase: Began tap count: 1 window: <UIWindow: 0x454520> view: <UIImageView: 0x468a60> location in window: {80.000000, 116.953613} previous location in window: {80.000000, 116.953613}
     
  5. macrumors 68030

    johnnyjibbs

    #5
    Use the viewWithTag method that UIImageView inherits from UIView.

    Tagging views enables you to keep track of them. When you set up a view, assign the tag property to a unique number:

    e.g.
    Code:
    myView1.tag = 1;
    
    or better still...

    Code:
    #define TAG_FOR_VIEW_ONE 1
    #define TAG_FOR_VIEW_TWO 2
    
    ...
    
    myView1.tag = TAG_FOR_VIEW_ONE;
    myView2.tag = TAG_FOR_VIEW_TWO;
    
    etc
    
    Then, when you need to access views:

    Code:
    UIImageView *myView1;
    myView1 = (UIImageView *)[self.view viewWithTag:TAG_FOR_VIEW_ONE];
    
    (self.view in the example above is the view for which the tagged view is a subview of).

    Use the touchesBegan, touchesMoved and touchesEnded methods to execute code when a touch occurs on any of these view. It would probably be best to make a custom subclass for your image views and then implement the touch methods there.

    Or, consider using a UIButton instead if you just want to implement button functionality.
     
  6. Moderator

    robbieduncan

    Staff Member

    #6
    Does the UIEvent not contain a pointer to the originating view?
     
  7. macrumors 6502

    iphonejudy

    #7
    I solved it by using

    if ([touch view] = imageView) {

    }
     

Share This Page