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
-
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? -
@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')
-
-
tf
is aui.TextField
so the "hooking up" is done on the linetf.delegate =
... -
@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 -
@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')
-
@cvp
Well done ππ»
I was thinking it will be nice to get this in the documentation β¦.. -
@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.