omz:forum

    • Register
    • Login
    • Search
    • Recent
    • Popular

    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?

    Pythonista
    uikit objcutil objc
    2
    4
    4488
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • ProfSpaceCadet
      ProfSpaceCadet last edited by ProfSpaceCadet

      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.

      1 Reply Last reply Reply Quote 1
      • blmacbeth
        blmacbeth last edited by

        I know this! I made a post a while back when I was experimenting with objc_util. The basic idea is to get the SUIViewController (a special Pythonista subclass of UIViewController) 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

        1 Reply Last reply Reply Quote 1
        • ProfSpaceCadet
          ProfSpaceCadet last edited by

          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.

          1 Reply Last reply Reply Quote 0
          • blmacbeth
            blmacbeth last edited by

            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')
            
            1 Reply Last reply Reply Quote 0
            • First post
              Last post
            Powered by NodeBB Forums | Contributors