To separate action methods out of the app delegate, I moved them into modules in the relevant controllers and included the modules in AppDelegate.
However at first, I tried to define the actions for each controller in a hash and use define_method
to dynamically create the methods. That gave me the error method '<action>:' created by attr_reader/writer or define_method cannot be called from Objective-C. Please manually define the method instead (using the 'def' keyword).
Compared to define_method
, including modules not very efficient with RubyMotion. For this situation performance is not as important, but is there another way to create methods on-the-fly that can be called from Objective-C?
From what I can understand from your explanation of your problem, you're handling actions in your app delegate. It's important to handle this kind of stuff from your controllers or to put the actions in your views, then setup a delegate for your view.
When it comes to RM though, you will have to work around not being able to do things like this.
I can't think of a time where this would be the only way to do what you're trying. Using Napalm's suggestion is one way.
If you were creating lots of events for dynamic content or server provided content, you can just use a method that uses those values instead of defining a method's definition off them (as define_method is usually providing a very similar method for all the dynamic methods it defines). Just try using normal `
There will be lots of alternative solutions available to you.