Search code examples
iosdrawrect

External Display drawRect issue


I have an app that displays the nodes of a connected graph. When the user touches a node I highlight the paths to the adjacent nodes by showing a clearColor "connections" view behind the nodes that is sized to contain the nodes of interest. This view contains child views for each path and each of these path views has a drawRect method that draws a single line that appears behind the source node and each adjacent node. All well and good and this approach opens the potential for doing interesting things with the paths in the future.

However, I recently added code to detect an external display with the idea that the main display would be a zoomed in view inside a scroll view and the external display would show the global view of all nodes. It worked fine until I added the connection view to the external display to show what was selected by touch on the main display. My path views (and their parent and grand parent views) are identical as far as I can tell, never have their drawRect methods called, as do their main view cousins, after setNeedsDisplay has been called. They are both initialized in the same place and in the same way.

I've tried dozens (hundreds?) of things to isolate the problem, but haven't figured out the problem. If I add a generic UIView to the connections view it appears in both places, but my path view, which extends UIView by adding a drawRect, only gets called on my main display and never on my external display.

I've run out of ideas. Does anyone have any ideas on what might be the cause of the problem?


Solution

  • I was pulling hair trying to figure this one out. Comparing the state of the two views did not show any difference. A few days ago I finally decided to write some a python routine to run in LLDB that would dump the state of the objects and then did a diff on the two views. It turns out that they were attempting to show the same subviews!

    In order to minimize object creation I was stuffing the path subviews into an array to avoid creating more than I ever needed and hiding them when not in use. But, I had stupidly declared this array outside of the @interface block and had inadvertently made it into a global and so both views were using the last set of subviews defined. Of course, a view cannot be a child of more than one parent view. Moving the declaration into the right place fixed the problem.

    Dumb error! But perhaps this will jog something for someone else with a similar problem.