tags:

views:

245

answers:

2

Im new to Obj-C, and I have a class, with a variable of a class in it, and I have a function not in a class that I need to call a function of the variable of the first one from, when the first class calls the function. Thats written all wrong, but I dont know the right words so:

Root.h:

@interface Root {
    UITableView *tableview;
}
@property (nonatomic,retain) UITableView *tableview;
@end

I have a function(Root is a delagate, so it gets called, and calls another function, not in any class or anything:

void Search()
{
    //here I need to call a function, I dont know how.  If I was in Root.m, it would be
    //[self.tableview reloadData];

But here I dont know how to call it, ive tried passing Search a pointer to a function in Root.m that calls the function:

-(void) update {
    [self.tableview reloadData];
}
-(void) anotherFunction {
    Search(update);

or

    Search(self.update);

etc but it always errors there, I also tried passing a pointer (self) to Search, and calling

[pointer.tableview reloadData];

but that errors when I run it:

unrecognized selector sent to instance 0x3detc

How Do I do it?

(sorry if this was very confusing)

A: 

I'm not sure I understand you correctly, but if you want to call a function defined in a class from outside the class you simply create an instance of the class if its an instance method and use that as the receiver, or if it's a class method, you use the class itself as the receiver. In other words suppose I want to call the method someFunction which is an instance method of SomeClass. Here's what I need to do:

SomeClass *someClass = [[SomeClass alloc] init];
[someClass someFunction];

If it's a class function, I don't need to instantiate an object instance. I simply do this:

[SomeClass someFunction];
ennuikiller
Yes. but wouldnt that create another instance of that class? That doesnt work, and gives the same error as passing a pointer to the class.
zacaj
Also your Search method appears to be in C instead of Obj-C. ALthough this is certainly acceptable, is there any reason not to code it as Objective C?
ennuikiller
I dont like Obj-c, but mostly its because the search method is supposed to be cross platform, and I dont want to have to rewrite it if a platform doesnt support obj-c. Its declared in a mm file anyway, though, since otherwise it wont compile.
zacaj
post the whole file or files. If you import the file containing Search, you shouldn't have a problem.
ennuikiller
A: 

So let me get this straight: you have a class Root and an instance of that class myRoot. In the class, there two functions -update and -anotherFunction. You also have a C function search(). When in -anotherFunction, you want to call search() and, from there, call back to -update in the original instance of root.

If that is all correct, let's pretend you got into -anotherFunction by calling [myRoot anotherFunction];. We'll also assume that everything is in the same file or that all files have been #imported or #included correctly.

You need to define search() as void search(id callback). In -anotherFunction call search() like this: search(self). Then, in search(), you can call [callback update]; to call -update from the class instance where you were originally executing -anotherFunction.

I think that answers your question. Please don't hesitate to ask for clarifications.

CajunLuke