omz:forum

    • Register
    • Login
    • Search
    • Recent
    • Popular

    Welcome!

    This is the community forum for my apps Pythonista and Editorial.

    For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.


    Getting notification on NavigationView go back?

    Pythonista
    4
    6
    4410
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • ccc
      ccc last edited by

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

      1 Reply Last reply Reply Quote 0
      • dgelessus
        dgelessus last edited by

        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.

        1 Reply Last reply Reply Quote 0
        • enceladus
          enceladus last edited by

          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

          1 Reply Last reply Reply Quote 0
          • ccc
            ccc last edited by 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()
            
            1 Reply Last reply Reply Quote 0
            • ccc
              ccc last edited by 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 <.

              1 Reply Last reply Reply Quote 0
              • omz
                omz last edited by

                @ccc I'll think about it.

                1 Reply Last reply Reply Quote 1
                • First post
                  Last post
                Powered by NodeBB Forums | Contributors