user1792771
user1792771

Reputation: 99

how to call a method from a class in another class

I'm working to a new app for mac osx where i'm using a drag and drop system to let the user to input some files [this part works well] and i have a tabelView where i would like to display the paths of files inputed. I have the next method in tabelViewController.m:

-(void)add{
    NSLog(@"da");
    [list addObject:[[Source alloc] init]];
    [tableView reloadData];
}

In the DropView.m i included the tabelViewController.h and i'm trying to call the add method but it does nothing:

#import "TableViewController.h"
.....
- (void)concludeDragOperation:(id<NSDraggingInfo>)sender{
    [self setNeedsDisplay:YES];
    TableViewController *tvc;
    [tvc add];
}

Can someone to figure out why it doesn't do anything ?

Edit1: Ok after I fallow the answers, my concludeDragOperation method looks like this:

- (void)concludeDragOperation:(id<NSDraggingInfo>)sender{
    [self setNeedsDisplay:YES];
    TableViewController *tvc = [[TableViewController alloc] init];
    [tvc add];
    [tvc rD];
}

rD is a method from tableViewController which contain the reloadData method. But it doesn't want to work it don't reload the table view. Any ideea ???

Upvotes: 1

Views: 81

Answers (2)

esreli
esreli

Reputation: 5071

it seems as if you missed a great chunk regarding how OOP and Objective-C work (seriously, no offense there).

What link is there between DropView.m and tableViewController.h do you have?

By typing TableViewController *tvc; all you are doing is creating a pointer. You are neither creating an object nor pointing to an object, you have just simply created a pointer that can eventually point to an object in memory of type tableViewController.

Solution:

What you will need to do, is to somehow create a link between the two classes. For instance, you could create a custom delegate method for DropView that could communicate with any class who uses that custom DropViewDelegate methods. So, you could create a delegate method that tells objects that follow that delegate protocol that you just concluded a drag operation. A tutorial how to do so can be found at my blog [it's a permalink].

I am happy to post code, or you can read it on my blog. Good Luck.

Upvotes: 1

Lews Therin
Lews Therin

Reputation: 10995

tvc needs to point to an actual object. [[tvc alloc] init] Otherwise you are simply calling add on nil. This doesn't cause your program to crash as you might expect in other languages. Try it out and see what happens.

Upvotes: 1

Related Questions