简体   繁体   中英

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];

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];

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 .

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM