1

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];
Jaume
  • 3,672
  • 19
  • 60
  • 119
  • have u got a tab bar or smth? – Horhe Garcia Apr 12 '12 at 19:15
  • Uh, your "function on class laodingViewController" *is* an instance method. Like any other instance method you need an instance of the class to call it, and the fields it accesses will be fields in the instance. Pass the right instance you get the right data. – Hot Licks Apr 12 '12 at 19:45
  • (Hint: Quit thinking just about how to call the methods and start thinking about what "state" needs to be communicated between your existing components to allow them to function in a coordinated fashion. You can't invoke the method until you have the data the method will access.) – Hot Licks Apr 12 '12 at 19:48

4 Answers4

2

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];
Eric Petroelje
  • 59,820
  • 9
  • 127
  • 177
  • that's it, loadingController was already allocated from my delegate, so AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate]; [appDelegate.loadingController incrementaProgres:0.1]; solves it! many thanks – Jaume Apr 12 '12 at 19:54
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.

Community
  • 1
  • 1
rohan-patel
  • 5,772
  • 5
  • 45
  • 68
1

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.

mprivat
  • 21,582
  • 4
  • 54
  • 64
0

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.

danh
  • 62,181
  • 10
  • 95
  • 136