Passing data between classes

I developed a quiz and everything works very well, but there is a thing I want to improve: My problem is that I have 3 view controllers. In the first view controller, the user selects one or multi-user mode.

The second ViewController is a quiz game. But now in the third ViewController (results screen) I need to know if the user has selected one or multi-user mode.

I do not know how to pass this boolean from ViewController 1 to ViewController 3.

At the moment, I have a boolean in each ViewController and just pass this variable from View1 to View2 and then to View3. But I do not like it. Is there a way I solve this with delegates? Or do you know another, better solution?

Thank you in advance

+4
source share
1 answer

The Model-View-Controller approach assumes that the boolean belongs to the model code of your application. It is a common idea to make your model a single:

QuizModel.h

@interface QuizModel : NSObject @property (nonatomic, readwrite) BOOL isMultiplayer; -(id)init; +(QuizModel*)instance; @end 

QuizModel.m

 static QuizModel* inst = nil; @implementation QuizModel @synthesize isMultiplayer; -(id)init { if(self=[super init]) { self.isMultiplayer = NO; } return self; } +(QuizModel*)instance { if (!inst) inst = [[QuizModel alloc] init]; return inst; } @end 

Now you can use the boolean value in your controller code: include "QuizModel.h" and write

 if ([QuizModel instance].isMultiplayer) 

or

 [QuizModel instance].isMultiplayer = YES; 
+6
source

All Articles