Re: [Pyobjc-dev] Passing selectors as parameters
Brought to you by:
ronaldoussoren
From: Just v. R. <ju...@le...> - 2003-05-12 18:36:41
|
Ronald Oussoren wrote: > > I have an NSListView whose double-click actions I would like to > > handle. NSListView.setDoubleAction_( action ) requires a selector. > > My problem is that I'm not sure how to pass a method as a selector. I > > would like to pass a class "instance" method (i.e. "def > > didDoubleClick( self ):") that belongs to the class I use as the > > listview's data source. What is the correct way to do this? I have > > tried a number of ways, and have searched for answers but have found > > no working solution yet. > > You can use MyClass.didDoubleClick_ (e.g. a reference to an unbound > method, although bound methods also work). (Note that PyObjC then uses the method to get at the selector, ie. the method name, it does not otherwise use the method.) > Another option is the > method name as a string ("didDoubleClick_"). Except you need to use the ObjC spelling then: "didDoubleClick:". I just tried, the underscore notation doesn't work. > You can use 'setTarget_(obj)' to set where the message should be send > to. Here's a snippet from the TableModel example (as of today, I checked in some changes): def awakeFromNib(self): ... self.tableView.setTarget_(self) self.tableView.setDoubleAction_("doubleClick:") # this also works, but you still need to set the target: #self.tableView.setDoubleAction_(self.doubleClick_) return self def doubleClick_(self, sender): # double-clicking a column header causes clickedRow() to return -1 print "doubleClick!", sender.clickedColumn(), sender.clickedRow() > Note that the names of action methods should end with an underscore. Just |