I've run out of ideas as to why this code crashes on the second display of the popover. I was trying to write custom code for displaying a popover, because I want to control more details of what happens when it is shown/dismissed, but I can't get the actual popover view display portion to be stable...this code crashes pythonista on me every time, almost always on the second attempt to show the popover:
import objc_util
import ui
def make_cgrect(x,y,w,h):
import objc_util
return objc_util.CGRect(objc_util.CGPoint(x,y),objc_util.CGSize(w,h))
def getUIViewController(view):
import objc_util
UIViewController = objc_util.ObjCClass('UIViewController')
UIView = objc_util.ObjCClass('UIView')
if isinstance(view,ui.View):
viewobj = view.objc_instance
elif isinstance(view,objc_util.ObjCInstance) and \
view.isKindOfClass_(UIView):
viewobj = view
elif isinstance(view,objc_util.ObjCInstance) and \
view.isKindOfClass_(UIViewController):
viewobj = view
viewResponder = viewobj.nextResponder()
try:
while not viewResponder.isKindOfClass_(UIViewController):
viewResponder = viewResponder.nextResponder()
except AttributeError:
return None
return viewResponder
def showPopup(sender, popup):
import objc_util
parentvc = getUIViewController(sender)
UIViewController = objc_util.ObjCClass("UIViewController")
vc = UIViewController.new()
vc.view = popup.objc_instance
vc.modalPresentationStyle = 7 # this is the popover style value
vc.preferredContentSize = objc_util.CGSize(popup.width, popup.height)
popovervc = vc.popoverPresentationController()
popovervc.sourceView = sender.objc_instance
popovervc.sourceRect = make_cgrect(0,0,sender.width,sender.height)
parentvc.presentViewController_animated_completion_(vc,True,None)
#### create main view
v = ui.View()
v.frame = (0,0,400,400)
#### create view to show as a popover
v2 = ui.View()
v2.frame = (0,0,200,50)
v2.background_color = (1.0,0.0,0.0,1.0)
#### put in a button to trigger the popover
b = ui.Button()
b.title = "Show Popup"
b.frame = (0,0,100,30)
b.border_width = 2
b.corner_radius = 3
b.center = (v.width*0.5,v.height*0.5)
b.action = lambda s: showPopup(s,v2)
#### present the main view
v.add_subview(b)
v.present(style="sheet")
Everything looks legit to me, and it does work the first time around, though apparently puts things in an unstable state. I must be doing something bad with a reference or something...but I've tried a bunch of different ways of storing things and it always crashes. Any more advanced objc_util gurus want to take a look?