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.


    About delegates

    Pythonista
    3
    8
    101
    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.
    • P
      philippe last edited by

      Hi there
      I understand the concept about delegates;
      The documentation shows examples all clear but I don’t know how to link - for example a class containing functions to drive textfield view - it in the GUI manager?

      cvp 1 Reply Last reply Reply Quote 0
      • cvp
        cvp @philippe last edited by

        @philippe Not sure that I correctly understand your request (usual for me 😉), but try this code, it could perhaps help you to understand how to use delegate.

        import ui
        
        class MyTextFieldDelegate (object):
            def textfield_should_begin_editing(self, textfield):
                return True
            def textfield_did_begin_editing(self, textfield):
                pass
            def textfield_did_end_editing(self, textfield):
                pass
            def textfield_should_return(self, textfield):
                textfield.end_editing()
                return True
            def textfield_should_change(self, textfield, range, replacement):
                v.name = f'textfield_should_change: {range} , {replacement}'
                return True
            def textfield_did_change(self, textfield):
                pass
        
        v = ui.View()
        v.frame = (0,0,600,400)
        v.background_color = 'white'
        
        tf = ui.TextField()
        tf.frame = (100,100,200,32)
        tf.delegate = MyTextFieldDelegate()
        v.add_subview(tf)
        
        v.present('sheet')
        
        P 1 Reply Last reply Reply Quote 0
        • P
          philippe @cvp last edited by

          @cvp
          Thank u @cvp
          So , I suppose I must have to link this class somewhere in the GUI when creating the view, right?

          ccc 1 Reply Last reply Reply Quote 0
          • ccc
            ccc @philippe last edited by

            tf is a ui.TextField so the "hooking up" is done on the line tf.delegate = ...

            P 1 Reply Last reply Reply Quote 0
            • P
              philippe @ccc last edited by

              @ccc
              It’s all right
              You must implement the hook in the code
              No way to do it during the creation of the view in the GUI manager
              Thanks

              cvp 1 Reply Last reply Reply Quote 0
              • cvp
                cvp @philippe last edited by cvp

                @philippe said

                No way to do it during the creation of the view in the GUI manager

                Sorry for that, I had not read enough your request. Now, I understand... Sincerely, I don't know.
                Anyway, you would have to write some code for the delegate actions but perhaps you could in the GUI manager tell you will use it.

                In the Custom Attributes of your TextField, in GUI Manager, use

                {'delegate':MyTextFieldDelegate}
                

                And your .py could be, without self

                import ui
                
                class MyTextFieldDelegate (object):
                    def textfield_should_begin_editing(textfield):
                        return True
                    def textfield_did_begin_editing(textfield):
                        pass
                    def textfield_did_end_editing(textfield):
                        pass
                    def textfield_should_return(textfield):
                        textfield.end_editing()
                        return True
                    def textfield_should_change(textfield, range, replacement):
                        v.name = f'textfield_should_change: {range} , {replacement}'
                        return True
                    def textfield_did_change(textfield):
                        pass
                
                
                v = ui.load_view()
                v.present('sheet')
                
                P 1 Reply Last reply Reply Quote 0
                • P
                  philippe @cvp last edited by

                  @cvp
                  Well done 👍🏻
                  I was thinking it will be nice to get this in the documentation …..

                  cvp 1 Reply Last reply Reply Quote 0
                  • cvp
                    cvp @philippe last edited by

                    @philippe Don't forget that doc has already been a big work for the developer. And if you search via Google for Pythonista, UI, Custom Attributes, you will find some examples.

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