Forum Archive

Get the name of the actual Navigation-View?

kami

Hi,

i am trying to use the navigation view example from this adress:

https://github.com/TutorialDoctor/Pythonista-Projects/tree/master/Projects/UI/Navigation%20View%20Tutorial

My questions are:

  1. How can i find out which ist the loaded view in the navigation window? So from this example is it:

connect('File','file_view')
connect('Edit','edit_view')
connect('Tools','toolbar')
connect('Web','webview'),

  1. How can i access an element like a label on this view?

Thanks a lot.

Cu kami

mikael

@kami, NavigationView is challenging overall, to the extent that I have never really understood its value over some custom navigation. You can Google ”pythonista NavigationView” to review some of the discussions.

That said, I think you need to track the loaded view manually. E.g. modify the connect function to store the loaded view or its name to a separate variable, maybe as an attribute of the NavigationView itself.

For accessing a specific label in a view, the standard way is to set the name attribute, either in the UI Editor or in the code where you create the label, and then locating it by my_view['my_label_name'].

enceladus

Here is a simple way to use navigation view. Create a list of pyui files for each screen and use this class to generate a navigation view.
https://gist.github.com/encela95dus/aff2b7f6eb909e27364c135b18e686c9

kami

Hi,

thanks a lot. I think will use the variant with the named labels and try to locate them.

The other example gives no information about the pushed label??

But thanks a lot.

Cu kami

kami

Hi,

just another question. Is there any way to find out that i am back on the home screen? I just can push and store the subviews but how do i know that i am back on the home screen?

Thanks a lot.

Cu kami

enceladus

You can use view name.
See https://gist.github.com/encela95dus/1ea94076e60e4d58f8a92915ce3d3e0d

kami

Hi thanks a lot for the answer. But how can i access in this example:

https://github.com/TutorialDoctor/Pythonista-Projects/tree/master/Projects/UI/Navigation View Tutorial

from a def function an element of the actual pushed_view???

I cannot find the elements??

Please give an example.

Thanks a lot.

Cu kami

JonB

In that example, anything that you wanted to do accessing the pushed view elements need to happen within connect. For example, he loads up a view with a tableview, so accesses the tableview, sets its datasource, etc.

After that, any interaction with the displayed view should happen within callbacks-- so, a button on a view would use its sender parameter to traverse up to the rootview, and do things there. Alternatively, you could load all the views at the start, rather than within connect, so that you can explictly reference them by a variable name. but then you need to manually keep track of what is being presented.

The problem with pythonista navigation views is that we don't get any delegate methods when the displayed view changes. So, you never know when the back button is pressed, for example. You could implement an update function in custom views, along with a positive update_interval -- update is only called for the top view, so you could detect when the back button is pressed that way.

JonB

here are a few functions which allow you to get the top, root or entire view stack of a navigationview

def get_top_view(navigation_view):
    '''returns the top (aka visible) view from a navigation view'''
    import ctypes
    return navigation_view.objc_instance.navigationController().topViewController().view().pyObject(restype=ctypes.py_object,argtypes=[])

def get_bottom_view(navigation_view):
    '''returns the top (aka root) view from a navigation view'''
    import ctypes
    return navigation_view.objc_instance.navigationController().bottomViewController().view().pyObject(restype=ctypes.py_object,argtypes=[])

def get_view_stack(navigation_view):
    import ctypes
    nc= navigation_view.objc_instance.navigationController()
    view_list=[]
    for vc in nc.viewControllers():
        view_list.append(vc.view().pyObject(restype=ctypes.py_object,argtypes=[]))
    return view_list

def pop_to_root(navigation_view,animated=True):
    '''pops all views to return to the original root view'''
    navigation_view.objc_instance.navigationController().popToRootViewControllerAnimated_(animated)
kami

Hi,

thanks a lot for the answer. This works but Pythonista is always crashing when i run get_top_view???

Cu kami

JonB

try this version, which may be more robust...
https://gist.github.com/3033d2f71d6913b5b82e6de818c5764e

one thing to note: navigation views should not be presented inside panel, unless you swizzle SUI3_NavigationView to include. willDismiss_ method.