Sub views of NavigationView are not accessible
Using the UI editor I have created an instance of a "NavigationView" with several "Buttons" as subviews to push the child views. So far I had handled the control of the sub views individually, meaning I explicitly hardcoded the calls to push_view. Now I tried to this generically by accessing the button views using the "" operator and the buttons' names. However, none of the buttons could be found by name. Using the "subviews()" method I tried to iterate over the subviews only to find that the NavigationView does not seem to have a single sub view. Is this intended? Are NavigationViews treated in a special way when it comes to view hierarchy? Or is this a bug?
f.read()returns a string,
pprintdoesn't do bupkis here.
;)The proper way to prettify the JSON would be to
json.loadit, and then
However, if all he's concerned about is exporting, formatting doesn't matter, he just needs text to copy/paste
If he is on 1.6 he can just rename the pyui file with a 'json' extension , then can rename it back again with the 'pyui' extension
The issue seems to be something like the following...
When starting from a new pyui file, if you add a navigation view and then add views within the nav view using the UI designer, those views do not appear as .subviews. Where are they? I think that this is essentially an attempt at creating a root view of the nav controller. Also an issue with this approach is that any subsequent views pushed to the stack will underlay the buttons and touch event on the pushed view will not work.
One solution could be to instead programmatically push another pyui file with your buttons onto the nav stack. However, this results in odd behavior because the view pushed will seem to be the second view on the stack. I.e. You will be able to navigate backwards to an empty view from the button view.
I can't seem to find any relevant example code.
yes it does appear that the "subview" of a navigationview in the ui editor sets the root view. But since you cannot easily access the rootview of a navigationview after it is created this leads to some awkward situations.
Here is a way to get the rootview of a nav view, which only relies on a small bit of
def get_navigationview_root(nv): o=[v for v in gc.get_objects() if hasattr(v,'navigation_view') and v.navigation_view==nv and not v.superview] return o
@JonB After the issue of NavigationViews has been dormant for a while I picked it up again and tried your little helper function. Unfortunately, it did not work for me since the object set always seems to be empty for navigation views.
turns out you must present the view before this trick works. here I created a view in the ui editor, added a navigationview, then added a subview to the navigation view, to which i added a button. after loading then presenting, you can find the view which the nav view is showing. (this works for finding the active view of a navigation view, since other views will not have the navigation_view attribute pointing at the nav view object). If you gave the view a unique viewname, you could filter by name and that should work before the nv is presented. It would also be possible to store a list of views from gc before and after loading the view, and then filtering by name, or views with no superviews, etc. just make sure you delete the list so you don't hang onto references to discarded objects.
import ui,gc v = ui.load_view() nv=v.subviews def get_navigationview_root(nv): o=[v for v in gc.get_objects() if hasattr(v,'navigation_view') and v.navigation_view==nv and not v.superview] if o: return o root=get_navigationview_root(nv) print root #None v.present('sheet') root=get_navigationview_root(nv) print root #<_ui.View object at 0x48a0c34>
o=[v for v in gc.get_objects() if hasattr(v,'navigation_view') and v.navigation_view==nv and not v.superview] if o: return o
This is probably a place where a list comprehension is not your friend.
v for v in gc.get_objects(): if hasattr(v, 'navigation_view') and v.navigation_view == nv and not v.superview: return v return None # explicit is better than implicit
allows you to stop building a list of all gc objects immediately after you have found the one that you want.