No way to end editing
When running a script with a UI, there is no button to end editing/dismiss the keyboard.
This applies at least to iOS 9 running on iPhone 6.
Only way to dismiss the keyboard is to have something else the user can click, which is not elegant nor aligned with the user experience in e.g. Safari or when running a script in iPad (where a dedicated dismiss keyboard button is available).
Does anyone know a workaround?
Here is a way to access the inputAccessoryView, in the beta.
# coding: utf-8 from objc_util import * import ui '''set up top level view and textfield''' v=ui.View() tf=ui.TextField(frame=[0,0,400,30]) v.add_subview(tf) tfobj=ObjCInstance(tf) '''set up toolbar''' keyboardToolbar=ObjCClass('UIToolbar').alloc().init() keyboardToolbar.sizeToFit() flexBarButton=ObjCClass('UIBarButtonItem').alloc().initWithBarButtonSystemItem_target_action_(5,None,None) doneBarButton=ObjCClass('UIBarButtonItem').alloc().initWithBarButtonSystemItem_target_action_(0,tfobj,sel('endEditing:')) keyboardToolbar.items = [flexBarButton, doneBarButton] tfobj.textField().inputAccessoryView= keyboardToolbar v.present()
Thanks! "New to Pythonista" question: How do I get access to objc? Is this Xcode only?
There is a beta version of the upcoming 1.6.
Search the forums, there is a link to the signup request form.
Thanks omz, I got access to the beta and am now trying to understand JonB's sample above.
Any pointers on relevant objc usage documentation and samples?
Not having an ObjectiveC background, I am really starting from basics here. E.g. I have a TextView, not a TextField, so what method should I use in the final binding call? It is not textField(), nor did an experimental textView() work.
Just remove the call to textField, tfobj directly supports inputAccessory view.
As to how to go about learning objc in python... that's tricky. maybe find some simple example objc from stackoverflow, etc, and start converting it line by line to python. The tricky thing for me is always the named constants, often have to look up the objc header files, though some are available using ctypes in_dll.
I can follow your sample and read Apple docs in order to add more buttons to the bar, but I cannot get a call through back to a Python handler function when a button is clicked. All my attempts fail with Pythonista crashing.
All ideas would be highly welcome.
i changed TextField to TextView, and tfobj.textField() to simply tfobj, and this code worked.
Yes, sorry for being unclear.
Your code works beautifully. My problem comes when I try to add another button to do something else -- to take advantage of all that screen space -- I don't know how to get the button to call a handler function in Python.
Ok, found a working solution in the MapView objc sample:
Need to use a dummy ui.Button and set the Python click handler function as its "action". Then can use the button as the target and sel("invokeAction:") as the action when defining the Objc accessory button.