Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Mobile Development > BOOK: Beginning iOS Game Development
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old April 24th, 2012, 10:42 PM
Registered User
Points: 8, Level: 1
Points: 8, Level: 1 Points: 8, Level: 1 Points: 8, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
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.

thanks,
hugh
here is the code
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 
                                               CardBase:tCurrCardBase
                                                 andTag:x];
            x +=1;
            
            // Configure gesture recognizer
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] 
                                           initWithTarget:self 
                                           action:@selector(handletap:)];
            tap.numberOfTapsRequired = 1;
            
            [card addGestureRecognizer:tap];
            
            // Configure gesture recognizer
            UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] 
                                           initWithTarget:self 
                                           action:@selector(handlePan:)];
            //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;
    

}
Reply With Quote
  #2 (permalink)  
Old April 25th, 2012, 12:59 PM
Friend of Wrox
Points: 1,671, Level: 16
Points: 1,671, Level: 16 Points: 1,671, Level: 16 Points: 1,671, Level: 16
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2010
Posts: 298
Thanks: 1
Thanked 62 Times in 60 Posts
Default

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?

Bob
Reply With Quote
The Following User Says Thank You to thepianoguy For This Useful Post:
hughisheretoo (April 25th, 2012)
  #3 (permalink)  
Old April 25th, 2012, 01:36 PM
Registered User
Points: 8, Level: 1
Points: 8, Level: 1 Points: 8, Level: 1 Points: 8, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
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.
hugh


Code:
#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;
}

@end
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

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 08:34 AM
unspecified method problem? nerssi BOOK: Professional Jakarta Struts 0 September 12th, 2004 06:40 AM



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


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.