need help with objc_util
I've started dabbling with objc_util module by trying out sample code provided with Pythonista and reading iOS docs. I find the whole idea of using Objective C from Python exciting. I've managed to set a property like backgroundColor on ui.View. What's struck me is the long time it takes. Almost 3x as long as for normal ui.View. Besides, first I get the default 'vanilla' view and then it gets colored. Any ideas what I am missing here?
from objc_util import * import ui import time # WORKS THE SAME WAY as in Myclass below #cl = UIColor.alloc() #cl = cl.init().initWithRed_green_blue_alpha_(.2, .3, .6, 1.).autorelease() class Myclass (ui.View): def __init__(self): #ui.View.__init__(self) #changes nothing ocv = ObjCInstance(self._objc_ptr) cl = UIColor.blueColor() ocv.backgroundColor = cl start = time.clock() v = Myclass() v.present('sheet') print time.clock()-start start = time.clock() v2 = ui.View() v2.background_color('blue') v2.present('sheet') print time.clock()-start
Thanx in advance.
When you interact with UIKit via
objc_util, you have to call most methods on the main UI thread. Otherwise, you'll see strange behavior (e.g. long delays or crashes).
objc_utilprovides the handy
on_main_threaddecorator for this purpose:
from objc_util import * class Myclass (ui.View): @on_main_thread def __init__(self): ocv = ObjCInstance(self) #note: _objc_ptr isn't necessary. cl = UIColor.blueColor() ocv.backgroundColor = cl # ...
Thank you, Ole! It worked like charm, which again proves the point that reading the docs isn't enough. You need hands-on experience. I was too dumb to put 2 and 2 together despite having read about the
Your remark about redundant _objc_ptr was an eye-opener. I'm upset 'cause I spent quite some time to dig it out )))) Thanks again.