1

Okay so let's say I have class A...

CLASS A has a method that gets called as soon as the user makes an in app purchase.

-(void) didMakePurchase { ...  }

(VIEW CONTROLLER) CLASS B is the View Controller for the current scene. Inside class B I have a function that creates a UIAlertView that basically thanks the user for making the purchase.

-(void) createAlertViewAfterSuccessfulPurchase {  ...create UIAlertView... }

Goal/Problem: I want to be able to call the createAlertViewAfterSuccessfulPurchase method inside the didMakePuchase method which is in Class B.

What I've Tried: I tried importing class A and creating an object of Class B in Class A just so I could call that method, but it didn't work (my guess is because Class B is a view controller).

The Man
  • 1,462
  • 3
  • 23
  • 45

3 Answers3

2

Post an NSNotification in class A, and in class B add an observer to this notification

Omar Abdelhafith
  • 21,163
  • 5
  • 52
  • 56
1

Solution: Make Class B a delegate of Class A and then do:

[myDelegate createAlertViewAfterSuccessfulPurchase:myParams]

To declare the delegate:

In class A:

.h

@protocol myProtocol;

@interface ClassA : UIView
{

}

@property (nonatomic, assign) id<myProtocol> delegate;

@protocol myProtocol <NSObject>

- (void)createAlertViewAfterSuccessfulPurchase;

@end

.m

self.delegate = classBInstance.

to call:

[delegate createAlertViewAfterSuccessfulPurchase]

in Class B:

.h

@interface ClassB : NSObject <myProtocol>

.m

implementation of:

-(void) createAlertViewAfterSuccessfulPurchase {  ...create UIAlertView... }
Oscar Gomez
  • 18,436
  • 13
  • 85
  • 118
  • How would I make class B a delegate of Class A? – The Man Jun 28 '12 at 14:08
  • I got it all working, but the method doesn't get called... But I get no errors... – The Man Jun 28 '12 at 15:20
  • @TheMan messaged send to nil do not cause error in obj-c, so check that your delegate has been set and is not nil before calling your method. – Oscar Gomez Jun 28 '12 at 17:31
  • +1 I prefer delegates to provide handlers that must be used to process events, rather than a notification which is a one-to-many broadcast. – Abizern Jul 01 '12 at 17:01
0

check this solution, for this the simplest way is to use the NSNotificationCenter.

The current example is here (don't let the title confuses you, it is not about the delegates)

Delegate - How to Use?

but if there is connection between the two classes, I mean, your Class B creates the Class A, there is another way too, because you can also use the blocks in that case like this:

in your ClassB.m file:

- (void)startPurchase {
    [classAinstance didMakePurchaseWithFinishedBlock:^{
        UIAlertView *_alertView = [[UIAlertView alloc] initWithTitle:@"Title" message:@"Message" delegate:nil cancelButtonTitle:@"Dismiss" otherButtonTitles:nil];
        [_alertView show];
    }];
}

in your ClassA.m file:

-(void) didMakePurchaseWithFinishedBlock:(void (^)())finishedBlock { 
    ...

    finishedBlock();
}
Community
  • 1
  • 1
holex
  • 23,961
  • 7
  • 62
  • 76