I am trying to get certain iVars from my view controller to my view (a subview) so drawrect will draw a shape based on user inputs. When I hard code those variables into the subview, the shape draws perfect, but when I use getters to access the custom variables from the view controller, they come back nil and the drawing is messed up. I get no errors or warnings. I know there is a problem with the getter, any suggestions? Anybody have an example of passing variables to a subview so cgcontext can be used in the drawrect? I am stuck big time. I assume at this point my problem is so simple, I am missing something fundamental. I thought I had setters/getters down packed.
Make sure that whatever you're getting 'from' (ie, the parent, or view, or whatever you're accessing the getters on) is not nil. Try setting a break point right before accessing them and validating that your targets are valid, and have valid data.
We'd really need to see more code. Ben was saying that, somewhere in your view, you must set parent = (some code to get a reference to my parent)
. Make sure this actually works, i.e. you really do have a reference to your parent. Even if the ivars are correct, if your parent is nil, then sending [nil getSomeiVar]
will return 0 in most cases.
Without a better understanding of the code, this is my guess. Somewhere in your UIViewController, you should have something like:
myView = //make your view
[myView setParent:self]
Parent is a variable of the view, with a property so that you can access it
Edit: Why does it matter if we know what you are making?
For setParent to work, you need to following:
- an ivar
YourUIViewControllerSubclass *parent;
in your view's .h - a property for this ivar
@property (nonatomic, retain) YourUIViewControllerSubclass *parent;
in your view's .h - A synthesis for this ivar
@synthesize parent;
in your view's .m inside the@implementation
Yeah, you were both wrong, but I took a lot away from you helping me though. I didn't have to post code after all. I had to pick up my debugging skills big time. Someone in another post mentioned violations of the MVC. I had to set the ivars in the uiview, to the custom values FROM the view CONTROLLER. In other words, I had to set from the controller rather than getting from the subview.