Custom UIView subclass is not being passed a variable.

Discussion in 'iOS Programming' started by Neotyguy40, Nov 6, 2011.

  1. Neotyguy40 macrumors regular

    Joined:
    Jul 15, 2009
    #1
    I've only been using Obj-c for a week now, so bear with me, much of this may sound simple to you guys, and I may not understand the answers you give.

    I'm trying to create a way of displaying objects in a single view for a game by drawing from PNG images using CoreGraphics.

    I've created my own viewController that loads a custom view (CameraView) and draws to that view in scheduled loop:

    Code:
    //ViewController:
    
    - (void)loadView
    {
    	CameraView *playerView = [[CameraView alloc] init];
    	self.view = playerView;
    	[self initiateGame:(CameraView *)playerView];
    }
    
    - (void)initiateGame:(CameraView *)playerView
    {
    	SceneObject *Player = [[SceneObject alloc] init];
    	Player.position.x = 100;	
    	Player.position.y = 100;	
    	Player.size.x = 64;
    	Player.size.y = 64;	
    	Player.imageName = @"Player.png";	
    	[playerView.objects addObject:Player];
    	[playerView checkObjects];
    	[NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(gameLoop) userInfo:nil repeats:YES];
    }
    
    - (void)gameLoop
    {
    	[self.view setNeedsDisplay];
    	//I'll add more to this later after I solve this problem...
    }
    In my CameraView, I alloc-init an NSMutableArray in the initialization. I also create my own drawRect method:

    Code:
    //CameraView
    
    - (void)drawRect:(CGRect)rect
    {
        // Drawing code
    	for (int i = 0; i < [self.objects count]; i++)
    	{
    		SceneObject *object = [self.objects objectAtIndex:i];
    		[[UIImage imageNamed:object.imageName] drawInRect:CGRectMake(object.position.x, object.position.y, object.size.x, object.size.y)];
    		[object release];
    	}
    }
    However, the CameraView isn't receiving the array that I passed from the ViewController. I get a warning saying that "objects is an unused variable" in my CameraView file. However, I have no idea what is wrong.
     
  2. jiminaus macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #2
    Can you show us the @interface for CameraView and the init method?
     
  3. Neotyguy40 thread starter macrumors regular

    Joined:
    Jul 15, 2009
    #3
    Init method:
    Code:
    - (id)initWithFrame:(CGRect)frame
    {
        self = [super initWithFrame:frame];
        if (self) {
            // Initialization code
    		NSMutableArray *objects = [[NSMutableArray alloc] init];
        }
        return self;
    }
    Interface:
    Code:
    @interface CameraView : UIView
    {
    	NSMutableArray *objects;
    }
    
    @property (nonatomic, retain) NSMutableArray *objects;
    
    @end
     
  4. MythicFrost macrumors 68040

    MythicFrost

    Joined:
    Mar 11, 2009
    Location:
    Australia
    #4
    It seems to be that the objects array isn't being initialised because you're calling init not initWithFrame on your CameraView class which is where you initialise the objects array.

    Try this:
    Code:
    CameraView *playerView = [[CameraView alloc] initWithFrame:CGRect(0, 0, 100, 100];
    (And fill in the appropriate x/y/w/h.)
     
  5. jiminaus macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #5
    Also you assigned the newly created NSMutableArray to a local value called objects, not to the property objects.

    The line should be this:
    Code:
      self.objects = [NSMutableArray array];
    
    I also changed form alloc/init to a convenience constructor so that the memory management work correctly.

    Alternatively you could have done:
    Code:
       NSMutableArray *o = [[NSMutableArray alloc] init];
       self.objects = o;
       [o release];
    
    or:
    Code:
      self.object = [[[NSMutableArray alloc] init] autorelease];
    
     
  6. Neotyguy40, Nov 6, 2011
    Last edited: Nov 6, 2011

    Neotyguy40 thread starter macrumors regular

    Joined:
    Jul 15, 2009
    #6
    I am still getting the warning, and the objects are still not being passed.

    Code:
    CameraView.m:20: warning: unused variable 'objects'
    I NSLogged the thing like crazy. What is happening is that while in the ViewController, the array gets created with an object, etc. Then in the CameraView subclass, nothing seems to be in the array.


    Okay, so I've changed my CameraView initializer to this:

    Code:
    - (id)initWithFrame:(CGRect)frame
    {
        self = [super initWithFrame:frame];
        if (self) {
            // Initialization code
    		NSMutableArray *o = [[NSMutableArray alloc] init];
    		self.objects = o;
    		[o release];
        }
        return self;
    }
    I don't really understand what that would do though. However, it does get me a step closer. After it draws it however, it crashes with EXC_BAD_ACCESS message in the main function.

    I didn't understand where you wanted me to put the first code section you posted though.

    EDIT: Found out when it's crashing. For some reason it doesn't like this line:

    Code:
    [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(gameLoop) userInfo:nil repeats:YES];
    It crashes when it is called on the second interval. See any reason for this?

    EDIT: Fixed this by creating another variable in CameraView called currentObject. This allows me to skip the release until the view is dealloc'd. Better memory management anyways, so I'm happy about that.
     
  7. jiminaus macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #7
    The [object release]; line here is wrong and shouldn't be there. You might want want to (re)view the Advanced Memory Management Programming Guide.

    And you're missing a [Player release]; line in initiateGame:.

    You're currentObject workaround is a hack. You won't need it if you do your memory management correctly in the first place.
     
  8. Neotyguy40 thread starter macrumors regular

    Joined:
    Jul 15, 2009
    #8
    Thanks for that, this will help a lot in the future.
     

Share This Page