views:

72

answers:

3

I had a method on my main view controller named "calculateThis".

This method was run, obviously, as

int newValue = [self calculateThis:myVariable];

when I run it from inside the view controller.

Then I created a static class and I need to run this method from there.

How do I reference this method from that class using just relative references, as super, superview, delegate, etc. I cannot use the class name defined on the delegate because this static class is used in several apps of mine.

I need to go up in the hierarchy, I imagine one level, and access the method there...

thanks.

A: 

If your intent is to create a class that you can use without initializing it, that's possible using class methods. For instance, if I want to make a class called MyClass with a doSomethingWith: method, I would define the following:

In MyClass.h:

@interface MyClass : NSObject {

}

+(void)doSomethingWith:(id)thisObject;

@end

In MyClass.m:

#import "MyClass.h"

@implementation MyClass

+(void)doSomethingWith:(id)thisObject
{
    // Your code goes here.
}

@end

To reference this method in another class, you can use the class object for MyClass like so:

[MyClass doSomethingWith:@"Hello, World!"];

This isn't really a typical Cocoa or Cocoa Touch design pattern, but can be handy for things like calculations.

Jeff Kelley
This should be a singleton under Cocoa convention. A class that's never instantiated sounds like it should just be a collection of functions instead.
Chuck
A: 

Are you talking about the superclass? If so, you use [super ...].

Shaggy Frog
+1  A: 

Define your utility methods in a category on NSObject or related subclasses of NSObject. Which you have done.

Adding (id)sender to your method will work. Then your method can reference the object that called it. Something like this.

+(int)calculateThis:(id)sender userInfo:(id)info;

then your call becomes.

int newValue = [NSObject calculateThis:self userInfo:myVariable];
falconcreek
my methods are on a category of NSObject. All them are marked with +, like in +(void)... So I can use them without instancing an object...my problem is how I access the class which called the method from within the method.
Digital Robot
Updated answer.
falconcreek
thankssssssssssssssssss!!!!!!!
Digital Robot
welcome. it occurred to me that you should be able to use the following statement to make it more clear in your code that you are using a class method defined in your category. int newValue = [NSObject calculateThis:self userInfo:myVariable];
falconcreek