Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Mobile Development > BOOK: Beginning iOS Game Development
BOOK: Beginning iOS Game Development
This is the forum to discuss the Wrox book Beginning iOS Game Development by Patrick Alessi ; ISBN: 978-1-1181-0732-4
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning iOS Game Development section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old April 24th, 2012, 10:42 PM
Registered User
Join Date: Apr 2012
Posts: 2
Thanks: 1
Thanked 0 Times in 0 Posts
Default 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;

Old April 25th, 2012, 12:59 PM
Friend of Wrox
Join Date: Aug 2010
Posts: 298
Thanks: 1
Thanked 62 Times in 60 Posts

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?

The Following User Says Thank You to thepianoguy For This Useful Post:
hughisheretoo (April 25th, 2012)
Old April 25th, 2012, 01:36 PM
Registered User
Join Date: Apr 2012
Posts: 2
Thanks: 1
Thanked 0 Times in 0 Posts
Default 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;


Similar Threads
Thread Thread Starter Forum Replies Last Post
Position() Method Problem kwilliams XSLT 3 May 20th, 2008 02:11 PM
What is best method to solve problem VictorVictor ASP.NET 2.0 Basics 5 July 20th, 2006 01:53 PM
Problem with a method? savoym C# 1 January 6th, 2005 09:34 AM
unspecified method problem? nerssi BOOK: Professional Jakarta Struts 0 September 12th, 2004 06:40 AM

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