Forum Archive

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

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 ?

JonB

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

Brun0oO

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

Brun0oO

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

Brun0oO