p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   BOOK: Beginning iOS Game Development (http://p2p.wrox.com/forumdisplay.php?f=667)
-   -   Problem with ViewWithTag method in UIView (http://p2p.wrox.com/showthread.php?t=87367)

hughisheretoo April 24th, 2012 10:42 PM

Problem with ViewWithTag method in UIView
I have code below basically variation from the Concentration game. The code is the method that adds the cards(UiImageViews) to the view. That works as it does in the game. Except that at the bottom of the method I attempt to make changes to one of the views that I have added.

As part of the process of adding a subview I set the tag property. I looked in the apple docs and found a ViewWithTag method for the UIView class. I was hoping that I could use that method to select one of the subviews that I had added to the view and simply change the location of one of the cards.

In stepping through the code with debug. it apparently does not find the card that has the tag 5. The Tag and all of the x and y etc variables are all zero.

Is what I am trying logically correct. Or is there something wrong with my code. It would be a powerful function for my app if I had the capability of selection specific cards and being able to modify their properties.

here is the code

-(void) addCardsInPlaceToView:(HO_Deck*) deck
    NSString *tCurrCardBase;
    CardImageView* card;
    CGRect cardFrame;
    CGRect cardOrigin = CGRectMake(0,0, 60, 80);
    cardFrame.size = CGSizeMake(60, 80);
    CGPoint origin;
    int x = 1;   
    NSTimeInterval timeDelay = 0.0;
    for (int i=0; i<4; i++) {
        for (int j=0; j<13; j++) {
            origin.y = i*80 + 110;
            origin.x = j * 60 + 150;
            cardFrame.origin = origin;
            // Create the card at the origin
            deck.currPtr = x; 
            tCurrCardBase = deck.currCardBase;
            // card = [[CardImageView alloc] initWithFrame:cardOrigin
            card = [[CardImageView alloc] initWithFrame:cardFrame
            x +=1;
            // Configure gesture recognizer
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
            tap.numberOfTapsRequired = 1;
            [card addGestureRecognizer:tap];
            // Configure gesture recognizer
            UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]
            //pan.numberOfTapsRequired = 1;
            //[pan numberOfTouches1 ];
            [card addGestureRecognizer:pan];
            [self.view addSubview:card];
    // card = - (UIView *)viewWithTag:(NSInteger)tag
    NSInteger tCard5 = 5;
    UIView * DispCard;
    DispCard = [self.view viewWithTag:(tCard5)];
    NSInteger tTag = DispCard.tag;
    float tx = DispCard.frame.origin.x;
    float ty = DispCard.frame.origin.y; 
    float th = DispCard.frame.size.height;
    float tw = DispCard.frame.size.width;


thepianoguy April 25th, 2012 12:59 PM

It will be helpful to see the code for your CardImageView initializer. The problem may be there. After you alloc and initWithFrame:CardBase:andTag check the frame on the card inside the loop. Does it have the origin and size you expect?


hughisheretoo April 25th, 2012 01:36 PM

got an answer
Thanks for the response. My code is below. I also submitted this problem to the Apple Developers forum and I got a response. You probably would have found it too if I had included the code below.

The Apple response said there were two problems one I had a tag variable defined in my CardImageView class. And I was in effect setting that tag variable instead of the view tag.

Also in my definition of the DispCard I should have use CardImageView instead of UIView. I told them I got an error or that and they gave me the following syntax to use and it all worked successfully.

CardImageView * DispCard;
DispCard = (CardImageView *)[self.view viewWithTag:(5)];

Thanks for your response. You probably would have found the same.


#import "CardImageView.h"

@implementation CardImageView

- (id)initWithFrame:(CGRect)frame CardBase:(NSString *)cardBase andTag:(int) inTag {
    NSString * tImageName;
    self = [super initWithFrame:frame];
    if (self) {
        self.tag = inTag;
        self.userInteractionEnabled = YES;
        tImageName = [NSString stringWithFormat:@"%@.png",cardBase];
        self.image = [UIImage imageNamed:tImageName];
    return self;


All times are GMT -4. The time now is 05:18 AM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.