0

In my drawing app I have a switch called "Snap to grid" -- When its state is changed I need drawing in my canvas view to honor that change. At present, the switch is a subview of a UIView so that the text label and the switch can travel together during a device orientation change.

I'm trying to decide what is the best way of communicating the state change. My thought train has been comparing this to using jQuery selector, like a $("#switch") -- in essence, when the view controller has instantiated the subviews (and subviews of those subviews), how do you get a pointer to the specific subview you're looking for?

I should add that my views are created in the Interface Builder portion of Xcode, so they are loaded from a .nib file rather than me creating them (which obviously would give me a chance to hold on to a pointer to the subviews I need).

tobinjim
  • 1,862
  • 2
  • 19
  • 31
  • what's the problem with declaring an IBOutlet? – Denis Nov 17 '11 at 21:08
  • Well, so far what's wrong with it is that I must not be trying it the right way. The IBAction for "snapToGrid" coming from the UISwitch to First Responder stubbed out the correct code in the view controller. But trying to create an IBOutlet for the boolean "snapping" in the draw view gives me the dreaded "not key value-coding compliant for the key:snapping" This doesn't necessarily surprise me because control dragging from the draw view to the view controller header file presented the connect box okay but I set the type to BOOL, which felt weird. – tobinjim Nov 18 '11 at 06:55
  • So while I get IBOutlets for UI elements, like the text field every tutorial on actions and outlets uses, I don't get it for internal control variables. – tobinjim Nov 18 '11 at 06:56
  • to handle the change event of your UISwitch, create an IBAction in controller and in Nib file connect didChange: event of the UISwitch with your Controller (which will be a File Owner, not a First Responder); if you want to control UISwitch state from the code, you need to create an UISwitch * ivar/property in your controller and link them in the Nib file as well – Denis Nov 18 '11 at 07:26
  • Thanks Denis. That work is already done. What I'm trying to do is get the boolean value now known to the controller over to the draw view so that when the touchesEnded method is called it knows to snap to grid (or not). If I created the view programmatically in the controllerView, I'd have a pointer to the view. But since it's a complex window I'm working with I used IB/nib and that's causing me the mental disconnect. – tobinjim Nov 18 '11 at 08:15
  • So, do you need any other assistance on this? Just let me know your exact question =) – Denis Nov 18 '11 at 08:23
  • Hehe. I'm still stumped. I tried having the touchesEnded method ask the controller whether the switch is set to on/off, but that looks like a bad design practice: http://stackoverflow.com/questions/1340434/get-to-uiviewcontroller-from-uiview-on-iphone – tobinjim Nov 18 '11 at 08:25
  • I'm back to now to having declared a method on the draw view that the controller can call telling the draw view to update the on/off state of snapToGrid. But an IBAction gets a (id)sender, so when I try to use something like [sender on] to get the value of its UISwitch on property, I need to typecast sender -- but I'm not getting that written correctly. Anything that I find this hard usually tells me I'm doing this all wrong. – tobinjim Nov 18 '11 at 08:27
  • you don't need to make typecasts for the id type. Just call BOOL boolValue = [sender isOn]; and that's will be it – Denis Nov 18 '11 at 08:31
  • Okay, got it done! I had to double-click on the draw view in the outline view in IB to make it selectable, and then control-click dragged from the draw view to the viewController.h to establish the IBOutlet. That allowed me in the viewController to send messages to it. Thanks, Denis, for piping in. – tobinjim Nov 18 '11 at 09:13

0 Answers0