Invalid Memory
Invalid Memory

Reputation: 737

Why can't I call method from another class?

I'm trying to call a method in "SettingsViewController" from "IAPHelper".

After searching for an answer, I thought I'd got the gist of how to do it. Here's what I've done:

SettingsViewController.h

@interface SettingsViewController : UITableViewController

- (void)stopActivityIndicatorAnimating;

SettingsViewController.m

- (void)stopActivityIndicatorAnimating
{
// DO STUFF
}

IAPHelper.h

#import "SettingsViewController.h"

IAPHelper.m

SettingsViewController *settingsViewControllerInstance = [[SettingsViewController alloc] init];
[settingsViewControllerInstance stopActivityIndicatorAnimating];

Now, I've searched around for people who had the same problem, but to me, the solutions all look like what I'm doing!

The problem I'm having is that there are no errors, the code should execute (I've added an NSLog) but the code inside the method it's supposed to be calling doesn't happen. It's just silently not doing it.

Could somebody please tell me if I've missed something out, done it all completely wrong (most likely) or if it really should work and I've done something wrong elsewhere, in which case I'll leave you all alone.

Thanks!

Upvotes: 0

Views: 154

Answers (3)

Joride
Joride

Reputation: 3763

In your IAPHelper, you are creating a new viewContorller. This is the object you send the stopActivityIndicatorAnimating message to. As this viewControllers view likely is not in the visible viewHierarchy, you will see no effect. The way to approach this is have a better design, e.g.: have your IAPHelper send out a suitable NSNotification. Then the viewCotroller can observe this notification and act on it (i.e. turn activityMonitor on or off). As your IAPHelper is most likely a data-layer kind of object, you don't want to couple it to the viewController, as this will lead to very rigid design, with problems like this becoming more frequent and more difficult.

Upvotes: 4

Clement Prem
Clement Prem

Reputation: 3119

As the other answers suggest you can have a better design than this.

From the above code what I feel is, you have created and started the animation in object A and trying create object B and executing the stop animation method in object B. You should pass the correct reference of the object (reference of object A) to IAPHelper and call the stop animation from that object.

Your IAPHelper should look like this

IAPHelper.h

#import "SettingsViewController.h"
@property (nonatomic, strong)SettingsViewController *settingsViewControllerInstance;

IAPHelper.m

[self.settingsViewControllerInstance stopActivityIndicatorAnimating];

Upvotes: 0

crizztus
crizztus

Reputation: 307

I'd suggest using NSNotification.

In your IAPHelper.m send a notification:

[[NSNotificationCenter defaultCenter] postNotificationName:@"StopAnimating"
                                                    object:self];

And in your SettingsViewController.m viewDidLoad method do:

[[NSNotificationCenter defaultCenter] addObserver:self
                                     selector:@selector(stopActivityIndicatorAnimating)
                                         name:@"StopAnimating"
                                       object:nil];

Using this code a notification will be sent from inside IAPHelper class to your SettingsViewController class where the method stopActivityIndicatorAnimating will be called.

Upvotes: 2

Related Questions