Forum Archive

ui module - how to utilize multiple top-level views in a single script

bitbucket158

I'm enjoying learning the new ui module and had the following question. How can I make use of multiple views? Meaning, multiple top level views which each save to a different ui file.

For example, suppose you want to have a loading "screen" and then the main ui "screen" and want to create them separately in the ui layout tool. Is this possible in pythonista?

Here's what I've tried without any luck.

1) Load and present the first view, then later load and present the second view. When doing this, I get an error about a view already being presented.

2) Same as above, but call close on the first view before calling present on the second view. This doesn't work and I get the same error.

3) Use a single view (which isn't what I'm going for but I thought I'd try it) composed of all the sub-views in the first and second view (this is the part where it is a pain in the layout editor). Then hide all the second view sub-views and just show the first set. When ready hide the first set and show the second set. This doesn't seem to do anything. Modifying the hidden attribute doesn't seem to have any effect.

4) Add and remove the loaded view beneath a single top level view. This doesn't seem like an option since the sub views attribute is read-only.

Anyway, just curious if anyone else knows how to approach this. I like the ui layout tool, but for a script with multiple stages (loading, entering data, showing results) its not going to work well unless I can setup separate views and then switch between them.

henryiii

There is also a navigation veiw, but I've only figured out how to add a single sub view (graphically).

bitbucket158

For showing a single view it works great. But if your app needs to switch between multiple views (maybe a loading view, then a display view, then an edit view, then maybe a settings view, etc) there doesn't seem to be an obvious way to do it.

omz

There are several things you could do, for example, have one container view, and add your actual content as a subview of that. When you want to switch to a different view, replace the subview with one you've loaded from elsewhere... You could also use a NavigationView (see here for an example).

bitbucket158

omz, how do I "replace the subview with one you've loaded from elsewhere"? The docs indicate that the subviews attribute of the View class is a readonly tuple. So how do I go about replacing a subview with another one?

omz

You can use the add_subview and remove_subview methods for this.

bitbucket158

Thank you! I was previously unable to find remove_subview in the docs, but I went back again and see it there. Thanks for the help :)