Domanda

My question is more of a design based question, but I am trying to learn something new so please don't close this. This might be little big, but I am trying to make it easier to understand what I am looking for:

I have 4 controllers i.e. ctrlA, ctrlB, ctrlC and ctrlD which gets called one after the other i.e. ctrlA calls ctrlB, ctrlB calls ctrlC and so on... ctrlA -> ctrlB -> ctrlC -> ctrlD.

I have a property in ctrlA whose value when set is required in ctrlC and ctrlD. One way I thought of doing it is passing it through the init methods of each and every controller which I understand is good only if we have limited properties. (I just have 1 property for now).

Code would be something like:

Class ctrlA:

@implementation ctrlA
{
   - (void)someMethod
   {
       BOOL isEmpty;
       if (somethingIs == true)
       {
           isEmpty = YES;
           ctrlB *controller = [[ctrlB alloc] initWithIsEmpty:isEmpty];
       }
   }
}

Class ctrlB:

@implementation ctrlB
{
    - (void) initWithIsEmpty:(BOOL)isEmpty
      {
        self.isEmpty = isEmpty; 
      }

    - (void) someMethodB
      {
        ctrlC *controller = [[ctrlC alloc] initWithIsEmpty:self.isEmpty];
      }
}

Class ctrlC:

@implementation ctrlC
{
    - (void) initWithIsEmpty:(BOOL)isEmpty
      {
        self.isEmpty = isEmpty; 
      }

    - (void) useIsEmpty
      {
        // Use self.isEmpty value in here to set text on a cell and also pass it to ctrlD in below method.
      }

    - (void) someMethodC
      {
        ctrlD *controller = [[ctrlD alloc] initWithIsEmpty:self.isEmpty];
      }
}

Class ctrlD:

@implementation ctrlD
{
    - (void) initWithIsEmpty:(BOOL)isEmpty
      {
        self.isEmpty = isEmpty; 
      }

    - (void) useIsEmpty
      {
        // Use self.isEmpty value in here to set text on a cell.
      }
}

Question - Is it okay to pass the property the way I described above? The other way I thought of giving a try is using protocol, but wasn't sure if even that could be the best way. Reason for that is, the property which I am trying to pass 'isEmpty' is a local property and is only set in one of the methods of ctrlA i.e. in 'someMethod' of ctrlA. Is there any other better way to pass properties to other controllers?

Nessuna soluzione corretta

Altri suggerimenti

Although it is certainly OK to pass values through initializers, a more direct approach would be to keep the shared value in your model object. The controller ctrlA would set the value in the model, and then controllers ctrlC and ctrlD would be able to grab it at their convenience.

Here is an answer illustrating this concept. Here is a question describing how to access a model class from multiple view controllers.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top