Jaume
Jaume

Reputation: 3800

objective-c call function from another class

I have a function on a class "loadingViewController" that needs to be accessed from other classes. First time that I call function like follows, it works but if I then call it again from another class do not because allocates it again and reset parameters. Same if I create an instance method. How to simply call a function from another class without init or allocate again? Probably basic newbie issue... Thanks.

class was declared in header and properly synthesized.

self.loadingController = [[loadingViewController alloc] initWithNibName:@"loadingViewController" bundle:nil];
[loadingController incrementProgress:0.1];

Upvotes: 1

Views: 1678

Answers (4)

danh
danh

Reputation: 62686

It looks like the reason you want to call a function on a view controller is to present the progress of a long operation to the user.

The more common approach is to have the view controller start the operation and then observe it's progress, updating the view accordingly.

Upvotes: 0

mprivat
mprivat

Reputation: 21912

I would do this:

-(void) loadingViewController
{
    if ( self.loadingController == nil ) {
        self.loadingController = [[loadingViewController alloc] initWithNibName:@"loadingViewController" bundle:nil];
    }
    [self.loadingController incrementProgress:0.1];
}

AND make sure you don't call [xyz loadingViewController] from any other thread than the main UI thread.

Upvotes: 1

rohan-patel
rohan-patel

Reputation: 5782

You can implement protocols here. Protocols are used to call methods of another class from one class. In general it will define the set of methods which your class will implement. TO see how to implement it you can see this answer.

Upvotes: 1

Eric Petroelje
Eric Petroelje

Reputation: 60549

Hard to say for sure without seeing more code, but I'm thinking you just need to make sure you only initialize the loadingController once:

if ( self.loadingController == nil ) {
    self.loadingController = [[loadingViewController alloc] initWithNibName:@"loadingViewController" bundle:nil];
}
[self.loadingController incrementProgress:0.1];

Upvotes: 2

Related Questions