Passing values from View Controller to View

Discussion in 'iOS Programming' started by sm4ck, Dec 18, 2009.

  1. sm4ck macrumors newbie

    Dec 2, 2009
    This should be easy but it is driving me nuts. I am trying to pass a value from a ViewController to a View but it seems to be getting forgetting the value. More specifically, I am trying to get the accelerometer value in my ViewController and then pass this to the View. It appears I am successfully getting the input data correctly and sending it to the View but when I initiate a method within the view the data appears to be uninitialized. Any help would be appreciated.

    Code details following. I have tried to remove code that is not relevant for ease of reading but please let me know if you need more info.


    #import <UIKit/UIKit.h>
    @class GameView;
    @interface GameViewController : UIViewController <UIAccelerometerDelegate> {
        GameView *game_view;
    @property (nonatomic, retain) IBOutlet GameView *game;

    #import "GameView.h"
    #import "GameViewController.h"
    #include "Globals.h"
    @implementation GameViewController
    @synthesize game_view;
    - (IBAction)closeGameView:(id)sender
    	[self.view removeFromSuperview];
    - (void)accelerometer:(UIAccelerometer*)accelerometer didAccelerate:(UIAcceleration*)acceleration
      game_view.acceleration = malloc(sizeof(acceleration));
    	// OK, this seems to work so far. I know that I am getting accelerometer
      // readings and it does seem to be passing it to the GameView
      [game_view setAcceleration:acceleration];

    #import <UIKit/UIKit.h>
    #import <OpenGLES/EAGL.h>
    #import <OpenGLES/ES1/gl.h>
    #import <OpenGLES/ES1/glext.h>
    @interface GameView : UIView
      UIAcceleration *acceleration;
    	// The pixel dimensions of the backbuffer
    	GLint backingWidth;
    	GLint backingHeight;
    	EAGLContext *context;
    	// OpenGL names for the renderbuffer and framebuffers used to render to this view
    	GLuint viewRenderbuffer, viewFramebuffer;
    	// OpenGL name for the depth buffer that is attached to viewFramebuffer, if it exists (0 if it does not exist)
    	GLuint depthRenderbuffer;
    	BOOL animating;
    	BOOL displayLinkSupported;
    	NSInteger animationFrameInterval;
    	id displayLink;
      NSTimer *animationTimer;
    @property (readonly, nonatomic, getter=isAnimating) BOOL animating;
    @property (nonatomic) NSInteger animationFrameInterval;
    @property (nonatomic, retain) UIAcceleration *acceleration;
    - (BOOL)createFramebuffer;
    - (void)destroyFramebuffer;
    - (void)setupView;
    !!! OK This is where the problem is. When I enter drawView, the values
    previously set are no longer set !!!!

    #import <QuartzCore/QuartzCore.h>
    #import <OpenGLES/EAGLDrawable.h>
    #import "AsteroidsView.h"
    #include "Asteroid.h"
    #include "Camera.h"
    #include "Game.h"
    #include "Globals.h"
    #include "MovableObject.h"
    #include "World.h"
    // MACROS
    #define DEGREES_TO_RADIANS(__ANGLE__) ((__ANGLE__) / 180.0 * M_PI)
    @implementation GameView
    @synthesize animating;
    @synthesize acceleration;
    @dynamic animationFrameInterval;
    // Updates the OpenGL view
    - (void)drawView
      static Game current_game;
      static Camera main_view;
      static World test_world;
    	// Make sure that you are drawing to the current context
    	[EAGLContext setCurrentContext:context];
    	glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer);
    	glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
    	// Setup model view matrix
      // Move back a bit
      glTranslatef(0.0, 0.0, -3.0);
      // Main game "loop" calls this method multiple times.
      // Check if the game is active, if so, go through major game steps.
      // Game not fully implemented yet
        // acceleration.x y, and z should have values but they do not!
        current_game.Update(acceleration.x, acceleration.y, acceleration.z);
    	glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);
    	[context presentRenderbuffer:GL_RENDERBUFFER_OES];
  2. Luke Redpath macrumors 6502a

    Nov 9, 2007
    Colchester, UK
    Why are you doing this?

    game_view.acceleration = malloc(sizeof(acceleration));
    Shouldn't you just be doing this?

    game_view.acceleration = acceleration;
    Also, are you setting the acceleration before or after drawView gets called? If after, you probably need to override setAcceleration to call [self setNeedsDisplay].
  3. sm4ck thread starter macrumors newbie

    Dec 2, 2009
    Thanks Luke. I tried this but still no luck. I might not be fully understanding your suggestion.

    I thought the memory allocated might be getting overwritten somehow so wanted to malloc to ensure it was there. I took that out per your suggestion. I also just used [game setAcceleration: acceleration] instead of game_view.acceleration = acceleration

    OK, I put setNeedsDisplay right after the setAcceleration but still having the same problem. My My code now looks like this:

    - (void)accelerometer:(UIAccelerometer*)accelerometer didAccelerate:(UIAcceleration*)acceleration
      [game_view setAcceleration:acceleration];
      [game_view setNeedsDisplay];
    I am still having the same problem. Am I missing something here? Any input would be appreciated.

  4. sm4ck thread starter macrumors newbie

    Dec 2, 2009
    OK, I seem to have solved this by moving the accelerometer logic to the View. Not sure if this is the optimal solution but it seems to work.

Share This Page