-2

I am working on feedback kind of iPhone app. Where I am getting stuck for getting values from other class. Suppose I have three view controller classes "Class-A", "Class-B" and "Class-C". And I have created delegate for class B and C.

Initially my root is Class-A. So from Class-A, I am presenting Class-B and by using class B.delegate = self, I am calling Class-B delegate method in Class-A. And also there is no connection between Class-B and Class-C. So is it possible that if I will come from Class-C to Class-A then same Class-B delegate method will invoke again?

A method that I want to invoke in Class-A is -

-(void)saveAllVisitOptions:(SignIn *)signIn
{
    Store *store1 = [[Store alloc] init];
    store1.signINN  =[[SignIn alloc] init];
    store1.signINN = signIn;
    [resultArray replaceObjectAtIndex:indexValue withObject:store1];

    [Util setArrayPreference:resultArray forKey:@"Store"];
}

So if I will use singleton for this above method so will it work properly? I have searched all over but unable to find solution for that.

halfer
  • 19,824
  • 17
  • 99
  • 186
Anand Gautam
  • 2,541
  • 3
  • 34
  • 70

2 Answers2

0

You have to define some mechanism to know that the control is going from Class-C to Class-A, either using a shared instance of a class or saving an entry in NSUserDefaults, then trigger the delegate method from Class-A.

Singleton (singleton.h)
    @property (nonatomic) BOOL isReturningFromClassC;

in Class-C, set
    [singletonInstance setIsReturningFromClassC:YES];

in Class-A
    if([singletonInstance setIsReturningFromClassC])         // Call delegate

  • singletonInstance is the shared-instance of the sinlegton
Ravi Sisodia
  • 776
  • 1
  • 5
  • 20
  • @RaviWhat you mean by mechanism? I have tried by using singleton. – Anand Gautam Dec 09 '13 at 08:26
  • Then define a variable and set it on/off as required. Like you can set it on in the Class-C at the last movement in the process of going to Class-A, and in Class-A check whether the variable is on. And call the delegate if the variable is on. – Ravi Sisodia Dec 09 '13 at 08:54
  • @RaviSisodia I'm not sure I understand what you mean ever. Please share some code to explain your answer. – Popeye Dec 09 '13 at 09:03
  • Anand Gautam: Using singleton class is the same thing what I told you is 'shared instance'. – Ravi Sisodia Dec 09 '13 at 09:41
  • Still don't understand what you are talking about. Why do you need to set a boolean flag. – Popeye Dec 09 '13 at 09:59
  • So, that you can determine that the control, that is coming to Class-A, is coming from Class-C; not from any other Class. As you stated - "if I will come from Class-C to Class-A then same Class-B delegate method will invoke again" – Ravi Sisodia Dec 09 '13 at 10:06
  • "As you stated"? I'm not the person asking the question. This can be done with out using a boolean flag. Seems like a lot of effort for something so simple. – Popeye Dec 09 '13 at 10:20
  • Hey Popeye. I'm answering it in the context as he asked. He used Singleton, I answered it at his level. If he knows NSUserDefaults, that will be a better way, and Some answered for NSNotifications, I think, that will be the best way. And Sorry, I thought you asked the question. Just Sorry. – Ravi Sisodia Dec 09 '13 at 10:31
0

You could do it like

- (void)someMethodYouCallInClassA
{
    // Simple and some may call none efficient way.
    // Personally not sure why some call it none efficient.
    ClassB *myClassB = [[ClassB alloc] init];
    [myClassB someMethodYouCallInClassB];
}

Or you could use a singleton which is what many would say to use. To do this

ClassA.h

@interface ClassA : NSObject

@property (nonatomic, strong) NSData *userData;

+ (ClassA *)sharedInstance;
- (void)myMethodInClassA
@end

ClassA.m

#import ClassA.h

@implementation ClassA

@synthesize userData = _userData;

static ClassA *classASharedInstance = nil;

+ (ClassA *)sharedInstance
{
    static dispatch_once_t instanceToken;
    dispatch_once(&instanceToken, ^{
         classASharedInstance = [[ClassA alloc] init];
    });
    return classASharedInstance;
}

- (void)myMethodInClassA
{ 
     // Do what it is want
}

@end 

Then in you classB you can just do

 - (void)method....
 {
     ClassA *myClassA = [ClassA sharedInstance]; // If the instance has already been set then no need to do this again.
     [myClassA myMethodInClassA];
 }

This will allow you to easily access classAs methods and properties. I have included userData as an example of sharing properties. If you set userData in one class you will be able to access that instance of userData in another class easily.

If you have any questions please ask.

Popeye
  • 11,839
  • 9
  • 58
  • 91
  • @PopeyeThanks for giving me time.Can u pls check my updated question ? – Anand Gautam Dec 09 '13 at 08:48
  • Yes a singleton would work where you want it. Using the same way I have done this in my answer you should be able to implement this easy enough. If you want anything else please just ask. – Popeye Dec 09 '13 at 09:03