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.
Return values of user-defined Objective-C classes
-
Hi, I'm trying to define a class method in
objc_util
that returns my customized UI class as follows:from objc_util import * def myButton(_cls, _cmd): cls = ObjCInstance(_cls) button = cls.alloc().init().autorelease() # customize the button ... return button MyButton = create_objc_class( "MyButton", ObjCClass("UIButton"), classmethods=[myButton]) if __name__ == "__main__": my_button = MyButton.myButton() ...
But the return value is
None
.I tried to set the return value to
button.ptr
, but it had no effect.
I also took into account theargtypes
andrestype
issues, but they were a bit too technical for me.What is the correct way?
-
@indoxan Not what you want, but I think (not sure) that you need first to create an instance of your class.
from objc_util import * def myButton(_self, _cmd): button = ObjCInstance(_self) # customize the button button.opaque = True MyButton = create_objc_class( "MyButton", ObjCClass("UIButton"), methods=[myButton], protocols=[]) if __name__ == "__main__": my_button = MyButton.alloc().init() print(my_button) my_button.myButton() print(my_button)
-
Thanks for the reply, @cvp.
I'm implementing the
myButton
method as a class method, so I don't think creating an instance is necessary.I wanted to define a class method that returns a customized instance, like
UIButton
'ssystemButtonWithImage:target:action:
method.But this story should be put aside for now. As you say, similar things seem to be happen even when defining them as regular methods.