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.


    [closed] how to subclass an Objective-C class using the objc_util module and its create_objc_class ?

    Pythonista
    2
    4
    2685
    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.
    • Brun0oO
      Brun0oO last edited by Brun0oO

      Hi,

      I'm creating a subclass of UIView using create_new_class.
      Here my sample code :

      # coding: utf-8
      
      from objc_util import *
      import ui
      import ctypes
      import os, sys
      
      
      load_framework('SceneKit')
      
      SCNView, SCNScene = map(ObjCClass, ['SCNView', 'SCNScene'])
      
      
      
      
      def debug(obj):
          print(obj, dir(obj))
      
      
          
      
      def touchesBegan_withEvent_(_self, _cmd, foo, bar):
          print('touchesBegan')
          pass
      def viewWillAppear_(_self, _cmd, foo):
          print('viewWillAppear')
          pass
      def viewWillDisappear_(_self, _cmd, foo):
          print('viewWillDisappear')
          pass
      def didReceiveMemoryWarning( _self, _cmd):
          pass
          
      methods=[touchesBegan_withEvent_, viewWillAppear_, viewWillDisappear_, didReceiveMemoryWarning]   
          
      @on_main_thread
      def main():
          main_view = ui.View()
          main_view_objc = ObjCInstance(main_view)
          main_view_objc.setUserInteractionEnabled_(True)
          subclassed_uiview_class = create_objc_class('MyUIView', UIView, methods=methods)
          subclassed_uiview_inst = subclassed_uiview_class.alloc().init()
          subclassed_uiview_inst.setUserInteractionEnabled_(True)
          width, height = ui.get_window_size()
          f = CGRect(CGPoint(0, 0), CGSize(width, height))
          main_view.flex = 'WH'
          flex_width, flex_height = (1<<1), (1<<4)
          scene_view = SCNView.alloc().initWithFrame_options_(f, None).autorelease()
          scene_view.setAutoresizingMask_(flex_width|flex_height)
          scene_view.setAllowsCameraControl_(True)
          scene_view.setUserInteractionEnabled_(True)
          subclassed_uiview_inst.addSubview_(scene_view)
          main_view_objc.addSubview_(subclassed_uiview_inst)
          main_view.name = 'Test' 
             
          main_view.present()
          
          
      if __name__ == '__main__':
          main()
      

      I want to use this subclass to implement some specific methods :

      touchesBegan_withEvent_
      viewWillAppear_
      viewWillDisappear_
      didReceiveMemoryWarning

      Actually these methods seem to be not derived/wrapped.
      For example, touchesBegan_withEvent_ is not called when I touch the screen
      despite the use of setUserInteractionEnabled_(True) in all subviews...

      Does someone have an idea ?

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

        I believe those are all methods of UIViewController, not UIView...

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

          @JonB ok, thank you !
          I think I'm beginning to understand my mistake...

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

            As I took the wrong direction, I close this topic.

            Brun0oO

            1 Reply Last reply Reply Quote 0
            • First post
              Last post
            Powered by NodeBB Forums | Contributors