Getting name of the imageview

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

  1. macrumors 6502


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

    I refered the document.

    and i saw

     *   – 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


    Staff Member

    Views don't have names.
  3. macrumors 68030


    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:

    // 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:

    // 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


    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?

    -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
    	UITouch *touch = [touches anyObject];

    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


    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:

    myView1.tag = 1;
    or better still...

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

    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


    Staff Member

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


    I solved it by using

    if ([touch view] = imageView) {


Share This Page