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.
How can I present a UIAlertController?
-
I am working on a Pythonista script that displays a UITabBarController, which contains multiple UINavigationControllers, containing UITableViewControllers. At some point I need to display a UIAlertController, but all the methods I've tried to present it either crash the app or push the UIAlertController to the navigation stack.
My code:
from objc_util import * from UIKit import * alert = UIAlertController.alertControllerWithTitle_message_preferredStyle_('Title', 'Message', 0) def alertActionCancel(sender): print 'Cancelled!' alertActionCancelBlock = ObjCBlock(alertActionCancel, None, [c_void_p]) retain_global(alertActionCancelBlock) alert.addAction_(UIAlertAction.actionWithTitle_style_handler_('Cancel', 1, alertActionCancelBlock)) myTableViewController.presentViewController_animated_completion_(alert, True, None) # This line crashes Pythonista
Thanks in advance, any help is greatly appreciated.
-
I know this! I made a post a while back when I was experimenting with
objc_util
. The basic idea is to get theSUIViewController
(a special Pythonista subclass ofUIViewController
) and use that to present the alert. Things tend to crash otherwise…Here is a link to my post, which has snippets of code that can get you started.
B
https://forum.omz-software.com/topic/2314/share-code-uialertcontroller
-
I have tried using
SUIViewConrroller
, but Pythonista still crashes.viewControllerForView_
seems to just return my original view controller.P.S. This seems to be more of a issue on iPad.
-
That's weird. I wrote and tested the script on my iPad. Did you run the exact script in the linked post? Should be the second script that doesn't crash.
Here is the code, if you don't care to guess.
B.
# coding: utf-8 from objc_util import * import ctypes import ui SUIViewController = ObjCClass('SUIViewController') UIAlertController = ObjCClass('UIAlertController') UIAlertAction = ObjCClass('UIAlertAction') def ok_pressed(sender): print 'OK pressed', ObjCInstance(sender), dir(ObjCInstance(sender)) alert = UIAlertController.alertControllerWithTitle_message_preferredStyle_(ns('My Alert'), ns('My Message'), 0) alert_action_block = ObjCBlock(ok_pressed, None, [c_void_p]) default_action = UIAlertAction.actionWithTitle_style_handler_(ns('OK'), 0, alert_action_block) alert.addAction_(default_action) ##rvc.presentModalViewController_animated_(alert, True) ## Stop Crashes retain_global(alert_action_block) def button_tapped(sender): super_view = sender.superview super_view_pntr = ObjCInstance(super_view) vc = SUIViewController.viewControllerForView_(super_view_pntr) vc.presentModalViewController_animated_(alert, True) view = ui.View(frame=(0,0,500,500)) view.name = 'Demo' view.background_color = 'white' button = ui.Button(title='Tap me!') button.center = (view.width * 0.5, view.height * 0.5) button.flex = 'LRTB' button.action = button_tapped view.add_subview(button) view.present('sheet')