views:

90

answers:

3

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.

A: 

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.

Ben Gottlieb
I know the ivars on the controller are not nil because they are used to set a label on said controller. Those labels are correct.
richieRich
can you call the getters from the debugger? Are these properties or standard getter methods?
Ben Gottlieb
Standard getter methods.
richieRich
can you step into them in the debugger?
Ben Gottlieb
I am not sure what you mean "step in".
richieRich
using the debugger, can you break just before your getter gets called, and step into the code to see what's happening.
Ben Gottlieb
I see, yes, I can use a breakpoint and step in now. Thanks for the help there. The iVars being got have the appropriate values at the breakpoint.
richieRich
at this point, I think we're pretty much stuck without some code snippets.
Ben Gottlieb
Does the view have to be a subclass of the viewcontroller? I can make it so, but it'll be a little work.
richieRich
a view cannot be a subclass of UIViewController.
Ben Gottlieb
Yeah, I found that out the hard way. Anyway thanks for the help. I'm gonna keep searching. The getter just doesn't get.
richieRich
A: 

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:

  1. an ivar YourUIViewControllerSubclass *parent; in your view's .h
  2. a property for this ivar @property (nonatomic, retain) YourUIViewControllerSubclass *parent; in your view's .h
  3. A synthesis for this ivar @synthesize parent; in your view's .m inside the @implementation
David Kanarek
My app is too simple to post code. It'll be a dead give away what I am making.
richieRich
It says the view does not respond to setParent. The viewcontroller doesn't either.
richieRich
Did this help you any or can you give us some more info to try to figure it out?
David Kanarek
A: 

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.

richieRich