title: iPhone MVC. Need some help with understanding how to correctly pass data from Controller to View
body: <p>A little background:</p>
  
  <p>I'm a C# developer starting to mess with the iPhone (have an idea for a simple 2D game). The only MVC programming I've done was for the web (ASP.NET MVC) so although I do have an understanding in MVC, I can't wrap my mind around one thing. Here's an example to illustrate.</p>
  
  <p>Say I have a simple app where all I want to do is display a big circle on the screen. I created a "View Based Application" and it gave me the basic classes to start with:</p>
  
  <p>MVCConfusionAppDelegate
  MVCConfusionViewController</p>
  
  <p>Now since I'll be doing some custom drawing (I know I can add a subview and show the circle that way, but this is just a sample of a larger piece) I've added a class called MyCustomView and in Interface Builder set the View of the MVCConfusionViewController to be a MyCustomView.</p>
  
  <p>Now here's the problem. I want to be able to set in code the size of how big the ball on the custom view should be. So I have a property on the MyCusomView like this:</p>
  
  <pre><code>#import &lt;Foundation/Foundation.h&gt;
  
  @interface MyCustomView : UIView {
      NSNumber *ballSize;
  }
  
  @property(nonatomic,retain)IBOutlet NSNumber *ballSize;
  
  @end
  
  #import "MyCustomView.h"
  
  
  @implementation MyCustomView
  
  @synthesize ballSize;
  
  -(void)drawRect:(CGRect)rect
  {
      CGContextRef context = UIGraphicsGetCurrentContext();
      [[UIColor redColor]set];
  
      float floatValue = [self.ballSize floatValue];
      CGRect ballRect = CGRectMake(50.0f, 50.0f,floatValue , floatValue);
      CGContextFillEllipseInRect(context, ballRect);
  
  }
  
  @end
  </code></pre>
  
  <p>Then, here's my MVCConfusionViewController:</p>
  
  <pre><code>#import &lt;UIKit/UIKit.h&gt;
  #import "MyCustomView.h"
  
  @interface MVCConfusionViewController : UIViewController {
      NSNumber *ballSize;
  }
  
  @property(nonatomic,retain)IBOutlet NSNumber *ballSize;
  
  @end
  
  #import "MVCConfusionViewController.h"
  #import "MyCustomView.h"
  
  @implementation MVCConfusionViewController
  
  @synthesize ballSize;
  
  - (void)viewDidLoad {
      [super viewDidLoad];
      MyCustomView *myView = (MyCustomView *)self.view;
      myView.ballSize = self.ballSize;
  }
  </code></pre>
  
  <p>And finally, the MVCConfusionAppDelegate:</p>
  
  <pre><code>#import &lt;UIKit/UIKit.h&gt;
  
  @class MVCConfusionViewController;
  
  @interface MVCConfusionAppDelegate : NSObject &lt;UIApplicationDelegate&gt; {
      UIWindow *window;
      MVCConfusionViewController *viewController;
  }
  
  @property (nonatomic, retain) IBOutlet UIWindow *window;
  @property (nonatomic, retain) IBOutlet MVCConfusionViewController *viewController;
  
  @end
  
  #import "MVCConfusionAppDelegate.h"
  #import "MVCConfusionViewController.h"
  #import "MyCustomView.h"
  
  @implementation MVCConfusionAppDelegate
  
  @synthesize window;
  @synthesize viewController;
  
  
  - (void)applicationDidFinishLaunching:(UIApplication *)application {    
  
      viewController.ballSize = [NSNumber numberWithInt:200];
      [window addSubview:viewController.view];
  
      [window makeKeyAndVisible];
  }
  
  
  - (void)dealloc {
      [viewController release];
      [window release];
      [super dealloc];
  }
  
  
  @end
  </code></pre>
  
  <p>As you can see, there's an ugly cast in my viewDidLoad method. I was hoping I'd be able to make the connection of the ballSize properties in IB, but it won't let me. </p>
  
  <p>So my question simply is, what's the correct way of passing this data from my view controller to my view without doing that cast? I know I'm missing something fundamental, but I just don't see it. Any help would be greatly appreciated!</p>
  
  <p><strong>EDIT:</strong> Here's the source code. <a href="http://bit.ly/uKyp9" rel="nofollow noreferrer">http://bit.ly/uKyp9</a> Maybe someone can have a look and see if I'm doing anything wrong.</p>