Trying to get custom class working...

Discussion in 'iOS Development' started by MountainDew, Apr 29, 2009.

  1. MountainDew

    MountainDew New Member

    Joined:
    Feb 23, 2008
    Messages:
    194
    Likes Received:
    1
    Device:
    iPod touch
    I'm just trying to implement a custom class in an app and I can't get it to work...

    There's two labels (turnPlayer1 & turnPlayer2) and a button (which controls changeTurns)

    Now there's a Player class that controls player1 and player2. I'm trying to switch turns (hiding alternate labels) based on player1.turn being YES or NO.

    Here's the code:

    Player.h
    Code:
    #import <UIKit/UIKit.h>
    
    
    @interface Player : NSObject {
    	BOOL turn;
    }
    
    @property BOOL turn;
    
    @end
    
    Player.m
    Code:
    #import "Player.h"
    
    
    @implementation Player
    
    @synthesize turn;
    
    @end
    
    classtestingViewController.h
    Code:
    #import <UIKit/UIKit.h>
    #import "Player.h"
    
    @interface classtestingViewController : UIViewController {
    	IBOutlet	UILabel		*turnPlayer1;
    	IBOutlet	UILabel		*turnPlayer2;
    	Player *player1;
    	Player *player2;
    }
    
    -(IBAction)changeTurns:(id)sender;
    
    @property (retain,nonatomic) UILabel *turnPlayer1;
    @property (retain,nonatomic) UILabel *turnPlayer2;
    
    @end
    
    classtestingViewController.m
    Code:
    #import "classtestingViewController.h"
    
    @implementation classtestingViewController
    
    @synthesize turnPlayer1,turnPlayer2;
    
    -(IBAction)changeTurns:(id)sender
    {
    	player1.turn = YES;
    	if (player1.turn) {
    		player1.turn = NO;
    		player2.turn = YES;
    		turnPlayer1.hidden = YES;
    		turnPlayer2.hidden = NO;
    	}
    	else {
    		player1.turn = YES;
    		player2.turn = NO;
    		turnPlayer1.hidden = NO;
    		turnPlayer2.hidden = YES;
    	}
    }
    
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
        // Release anything that's not essential, such as cached data
    }
    
    - (void)dealloc {
    	[turnPlayer1 release];
    	[turnPlayer2 release];
    	[player1 release];
    	[player2 release];
    	
        [super dealloc];
    }
    
    @end
    
    Now did I like initialize the player1/player2 wrong or something? Because before I checked player1.turn's status, I did player1.turn = YES, and the if still doesn't work. Any ideas?

    Also, how do I get player1.turn to equal YES when the app loads up?
  2. Pelaez-1

    Pelaez-1 New Member

    Joined:
    May 5, 2008
    Messages:
    822
    Likes Received:
    0
    Device:
    iPhone
    Here are some pointers.

    Code:
    -(IBAction)changeTurns:(id)sender
    {
        player1.turn = YES; //You are setting player1 turn to YES everytime the button is pressed
        if (player1.turn) {
            player1.turn = NO; //Then you set it to NO
            player2.turn = YES;//and player2 to YES
            turnPlayer1.hidden = YES;
            turnPlayer2.hidden = NO;
        }
        else { //Since everytime you press the button you turn player1.turn to YES, this will NEVER happen.
            player1.turn = YES;
            player2.turn = NO;
            turnPlayer1.hidden = NO;
            turnPlayer2.hidden = YES;
        }
    }
    You can implement -(void)viewDidLoad, and there have the variables initialize.
    I think it'd go like this:
    Code:
    - (void)viewDidLoad{
        player1.turn = YES;
        player2.turn = NO;
        [super viewDidLoad]
    }
    Make sure you comment/delete the first line in your changeTurns function.

    And you should be more consistent when writing your code, like, if you are gonna put brackets on a new line, do it for all, etc.

Share This Page