Forum Archive

Getting notification on NavigationView go back?

ccc

Can my app get Alerted when the user clicks back in a NavView?

dgelessus

I haven't tested this, but perhaps you could check when the topmost view goes off screen (using the on_screen property or the wait_modal method). This would of course require some custom code when pushing each view onto the navigation view.

There's probably a better way to do this with objc_util and the native UIKit APIs, but I know almost nothing about UIKit.

enceladus

May be you can implement your own navigation view which may not be difficult. Here is an example implementation. The program simple_navigation_view.py builds a navigation view from a sequence of pyui files. The program custom_simple_navigation_view builds the same directly. Note that the second program does not have much additional code and you can customize the pop to your need.

https://gist.github.com/ae9572e2d0ba1b1f99f8282cac3adaea

ccc

@omz Would it be possible for you to add a callback mechanism so that the root_view of a ui.NavigationView() receives a will_close() or a go_back() method call when the user clicks on back (< in the upper left of the nav view)?

ui.View.go_back() could operate like ui.View.layout() -- if the method is present then it gets called at the appropriate time.

import ui

about = '''Press "Tap me" and then back ("<" in the upper left) numerous times.
           Desired behavior: Colors remain constant (white, red, white, red)
           Actual behavior: Colors cycle forward (white, red, white, green)'''

colors = 'white red green blue grey'.split()


class ColorView(ui.View):
    def __init__(self):
        self.index = 0
        self.name = 'white'
        self.add_subview(self.make_button())
        frame = (0, -220, *(ui.get_screen_size()))
        self.add_subview(ui.Label(text=about, frame=frame, number_of_lines=0))

    def button_tapped(self, sender):
        sender.navigation_view.push_view(self.make_button())

    def go_back(self):  # this NEVER gets called :-(
        self.index -= 1

    def layout(self):
        self.subviews[0].frame = self.bounds

    def make_button(self):
        color = colors[self.index % len(colors)]
        self.index += 1
        return ui.Button(title='Tap me', action=self.button_tapped, bg_color=color, name=color)

    def will_close(self):  # this NEVER gets called :-(
        self.index -= 1


ui.NavigationView(ColorView()).present()
ccc

Thanks @dgelessus Your view.on_screen checking idea worked well at https://github.com/cclauss/f1_hack/blob/master/discover_view.py#L24. This is a ui for walking the hierarchy of any json data. It would still be much easier if views displayed in ui.NavigationViews received a will_close() or go_back() callback when the user presses <.

omz

@ccc I'll think about it.