Notification/Callback/Hooks when ui.View is presented or closed?
So, another question I've come upon while exploring how to make UIs with Pythonista: is there any way to find out when a view is presented or closed? I mean is there some way to hook a callback or attach a notification to when a non-custom ui.View is shown with present() or closed with close()?
Related: when showing a view as a sheet or panel, or even full screen, there can be a "X" button which will close the view, but we don't have direct access to that titlebar or button, do we? There's no way to intercept the action when that is pressed? It's not a superview of the main displayed view, I looked for that.
you can implement
on_screen. Also, the custom view
will_closegets called when the view will close. but i think only for the root view that was presented. I think
layoutusually gets called when first presented... but maybe only for flex views.
@JonB thanks...I was definitely thinking about doing it via update() with a delay of zero, and checking for on_screen. This is for non-custom views, since custom views definitely have the methods needed to know with did_load() and will_close(), etc.
I'm wondering if this is something that I really need to dive into the objc_util system to get notifications via the NSNotificationCenter system for observing objects. Or even the lower-level NSKeyValueObserving system. Has anyone has experience with that? I'm not up to speed on converting objective-c calling style to the python equivalents.
what specifically are you going for? are you interested in just 'sheet', or other types of present?
Somewhere around here, someone used objc to hijack the "X" button..
Mainly, I will need to be able to present a non-modal UI, most likely as a sheet, and have a different part of the UI be notified in some fashion when it closes.
I could, of course, implement my own notification system of some form, and use a custom view class for the sheet I present, so I have access to will_close()...but I was hoping not to have to make a custom view for this. Still, if that's the easiest way, perhaps I will.
I'm curious if anyone has actually hooked into the "normal" objective-C/Swift way of sending notifications around using the NSNotificationCenter via objc_util? Since that would make it pretty easy to attach notifications to changes in views.
cvp last edited by
Here is a way to capture all notifications:
This could be modified to observe a specific object, selector, etc. I believe there is a notification when a view is shown, but i didnt see an obvious one when the X is pushed.
The confusing thing about what you are asking is that
sheetis already basically modal, since it blocks anything else happening. in which case
wait_modalmight be what you want.
For truly non-modal "windows", you might consider the Overlay, found here. This lets you create resizable views that float, and you can use
connectto register actions that get called when the view is closed.
Finally, it would be possible to delete the standard X, and replace it with your own button.
gives me the X button on ipad when presenting as sheet. This might be different on iPhone, and might be different if you have leftbuttonitems or rightbuttonitems defined.
But this lets you, for instance, hide the default X, and define your own using right_button_items.
v=ui.View() def c(sender): v.close() print('closed') v.right_button_items=[ui.ButtonItem(action=c,title='X')] v.present('sheet') v.objc_instance.superview().superview().superview().subviews().subviews().hidden=True