Discussion Forums  >  Plugins, Customizing, Source Code

Replies: 9    Views: 61

mrDavid
BTMods.com
Profile
Posts: 3936
Reg: May 21, 2011
San Diego, CA
51,910
11/22/13 08:00 PM (12 years ago)

How do I call this method/message in a different file?

So I'm working on a particular plugin that is using the shake function. I need to be able to reference the code within my shake function in other plugins after importing the .h file where my motionBegan function resides. -(void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event; I've tried [self motionBegan]; to no avail, along with several other more complicated combinations, of which none work. Anyone know how to achieve this? Help would be appreciated! David https://buzztouchmods.com/market
 
chris1
Code is Art
Profile
Posts: 3862
Reg: Aug 10, 2012
Austin, TX
50,120
like
11/22/13 09:55 PM (12 years ago)
The answer depends on what you're code is trying to do. Does it rely on properties of the plugin, or could that entire method work in any plugin? If the latter, the 'how' is really easy - the why a bit more complicated. What you will want to do is change the first character from a "-" to a "+". Then, you can call the method from any class that imports your .h file by calling [pluginName motionBegan:motion withEvent:event]; The why has to do with the basics of object-oriented programming. In most programming circles, what we call "plugins" are called "classes". (The difference is a plugin connects a class to the BT control panel). Classes have two different kinds of methods in them - "instance methods" and "class methods". Instance methods apply to an object of that class, and class methods are general methods that can be called without reference to an object. A typical way of explaining this in the textbooks is to think of a class called "dog". Our dog class might have several properties (color, size, energy) and several methods (bark, wagTail, sleep, eat). We can create an instance of this class like so: Dog *sophie = [[Dog alloc] init]; We can set properties like this: sophie.energy = high; sophie.color = brown; sophie.size = large; Now, let's say that the "wagTail" method reduces the dog's energy, while the "sleep" method increases it. We can call these methods like so: [sophie wagTail]; [sophie sleep]; But, let's say that the "bark" method doesn't effect the dog's properties at all. In fact, any "dog" we create can use this method and it will perform exactly the same thing. The method might look like this: +(void)bark { NSLog(@"Bark Bark!"); } We don't actually need a "dog" object to call this method. We can call it simply like this: [Dog bark]; Notice that we don't start with "sophie" (a reference to our object). Instead we start with a reference to the class file itself. Also note that our method begins with a "+" rather than a "-". That tells the compiler that this is a class method, not an instance method. Clear as mud? Now, if we really do need an instance method (i.e., we need something that relies on our object's properties), then another approach is needed. You'll need to reference the same object across plugins, which would mean initializing the object in the appDelegate.
 
tb
buzztouch Evangelist
Profile
Posts: 2050
Reg: Nov 03, 2011
Oxford
32,300
like
11/23/13 04:32 AM (12 years ago)
I would trust Cheis on this one! Even I've got no idea what he's talking about!
 
mrDavid
BTMods.com
Profile
Posts: 3936
Reg: May 21, 2011
San Diego, CA
51,910
like
11/23/13 08:21 AM (12 years ago)
@Chris, spent an hour studying what you said, it goes along with some basic knowledge I have on objective-c, so I kind of understand it, but I attempted to do it the way you explained and "no-go" (one error in the line to call the method). I emailed you the app package (600KB) if you wanted to take a look to see if I did it wrong (but feel free to not look if you're busy, totally understand). Thanks for taking your time to explain it step by step, really helps, these kind of answers throw me a step forward in actual programming knowledge, much appreciated! David https://buzztouchmods.com/market
 
chris1
Code is Art
Profile
Posts: 3862
Reg: Aug 10, 2012
Austin, TX
50,120
like
11/23/13 10:17 AM (12 years ago)
So, after looking at your project for 2 seconds I figured out what's going on. It was the case of responding to your question too late at night apparently! The method you're calling: - (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event; is a system method tied to a UIViewController. Since that method was written as an instance method, you're not going to be able to change it into a class method and have it work properly. What you could do is write a class method to perform whatever action you're wanting the app to do, like so: +(void)performShakeAction { [self showAlert:@"Shake! Shake! Shake!" theMessage:@"Shake your phone-e!" alertTag:0]; } And then instruct people to write this code into their other screens: - (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event { if(event.type == UIEventSubtypeMotionShake) { [Shake_feature performShakeAction]; } } The advantage here is that people can decide whether each screen should use the shake feature or not, and can override it to perform a different action on that screen if they so choose (and know what they're doing). If you're really wanting to handle shake events once and the same for all screens, then you'd need to handle it through the appDelegate rather than through a plugin. Here's a SO thread on how to do that. http://stackoverflow.com/questions/10154958/ios-how-to-detect-shake-motion
 
mrDavid
BTMods.com
Profile
Posts: 3936
Reg: May 21, 2011
San Diego, CA
51,910
like
11/23/13 10:19 AM (12 years ago)
Chris Chris Chris... you rock! I understand. Thank you so much for explaining these things in details, really helps. Cheers, David https://buzztouchmods.com/market
 
Niraj
buzztouch Evangelist
Profile
Posts: 2943
Reg: Jul 11, 2012
Cerritos
37,930
like
11/24/13 10:46 AM (11 years ago)
In calendar years, Chris is a babe in these here software woods. In reality, Chris is an awesome programmer! He assimilates and comprehends technical information at a rapid pace. And then he can turn around and explain it coherently to us babes! Well done, Chris. I love seeing your rapid evolution along the software development path. You are a lesson-learned for those who want to "just pick-up" mobile development -- it has been done! Please try to be at the Vegas or San Diego meetups, it would be good to see you again. With sincere respect, -- Niraj
 
chris1
Code is Art
Profile
Posts: 3862
Reg: Aug 10, 2012
Austin, TX
50,120
like
11/24/13 10:49 AM (11 years ago)
Thanks Niraj. Doubt I'll make San Diego, but I'm gonna try hard to be at Vegas
 
mrDavid
BTMods.com
Profile
Posts: 3936
Reg: May 21, 2011
San Diego, CA
51,910
like
11/24/13 10:57 AM (11 years ago)
I'll do my best to be at Vegas too if you don't make it to SD. Thanks again, Chris has helped me tremendously in these past two years to grow my business and my own personal skill, albeit very small compared to his. He's more than awesome! Cheers! David https://buzztouchmods.com/market
 
tb
buzztouch Evangelist
Profile
Posts: 2050
Reg: Nov 03, 2011
Oxford
32,300
like
11/24/13 01:23 PM (11 years ago)
I'm trying my best to get to Vegas, but my app sales just aren't coming in. I need to get my next two apps out fast if I'm to be there at Vegas!
 

Login + Screen Name Required to Post

pointerLogin to participate so you can start earning points. Once you're logged in (and have a screen name entered in your profile), you can subscribe to topics, follow users, and start learning how to make apps like the pros.