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)
So let me get this straight: you have a class
Rootand an instance of that classmyRoot. In the class, there two functions-updateand-anotherFunction. You also have a C functionsearch(). When in-anotherFunction, you want to callsearch()and, from there, call back to-updatein the original instance ofroot.If that is all correct, let’s pretend you got into
-anotherFunctionby 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()asvoid search(id callback). In-anotherFunctioncallsearch()like this:search(self). Then, insearch(), you can call[callback update];to call-updatefrom the class instance where you were originally executing-anotherFunction.I think that answers your question. Please don’t hesitate to ask for clarifications.