Forum Archive

Question About Delegates in UI

AtomBombed

I am currently making a note writing application that will pop up as a sheet over the script that you are currently editing as a workflow to make it possible to write notes about your script, without needing to leave your script. (None of this matters, just some background.)

I am wondering how to make a delegate work for a view that has been made using the UI editor, and not created through the script as "ui.Button(title="button)". I really need this to use the delegate to find out when text has been inserted into a TextView. That way, every time the text changes on the TextView, I will have the delegate run the appropriate function to change the text of a label to display the total amount of characters on the TextView currently.

So basically: I want to figure out how to make delegates work for views created via UI designer, and not created through the script. That's all I need.

PLEASE POST CODE EXAMPLES, SO I CAN SEE WHAT IS GOING ON WITH THE CODE.

Much appreciated,
Sean.

Webmaster4o

Say you make the view in the UI editor. Name the textfield something specific. I'll assume it's called my_textfield. Now, with code, you can say

import ui 
v=ui.load_view()
text=v['my_textfield']
text.delegate=DELEGATE_NAME_HERE

You will have to write the delegate with code, in case that's what you're asking. You can't create the delegate in the UI editor.

ccc

You might want to check out the ui-tutorial in the Pythonista-Tools repo.

ccc
import console, ui

class MyTextViewDelegate(object):
    def textview_did_change(self, textview):
        console.hud_alert('{} characters'.format(len(textview.text)))

v = ui.load_view()  # the .pyui file needs a ui.TextView named 'textview1'
v.name = 'Character Count'
v['textview1'].delegate = MyTextViewDelegate()
v.present('sheet')
AtomBombed

OH MY GOSH! Thank you so much. I can't believe I didn't think they'do be listed as a list ready for "getitem". I kept getting a "getitem" error because I was using it in the wrong terminology. I really appreciate your help! This is so nice. You guys are the best.