Don Jones
Don Jones

Reputation: 9497

Best approach to have a common overlay across many screens in an iPhone application?

I'm creating an application whose main screen can consist of one of three Scenarios. Let's call them A, B, and C.

The functionality for these Scenarios are drastically different, as is their user interface. For discussion, let's say that each Scenario is a distinct kind of logic puzzle, and that they share no common UI elements, logic, or functionality between them.

I'd therefore LIKE to have each Scenario implemented in its own ViewController. A Scenario would be displayed modally over a "main menu," so the main menu would call something like [self presentModalViewController:ScenarioX animated:YES] in order to display the Scenario.

Here's the question: Each Scenario will have a specific bit of UI that's common across them - say, a countdown clock in a status bar at the top of the screen. That common UI will have its own logic, albeit minimal. It'll obviously have to run the countdown clock, perhaps display a "time's up" message when the clock runs down, and respond to a "back" button that closes the modal ViewController and returns to the main menu.

What's the best way to implement this in terms of code-reuse, memory-management, and so forth?

I'd appreciate a level of technical specificity, not just an "approach." For example, if the right answer is to create the status bar/clock as its own subclass of UIView, and then add that programmatically to the Scenario's view, fine: Does that mean the Scenario's ViewController has to run the clock, back button, and so forth?

Or is it possible to create a StatusClock ViewController which manages the status bar, clock, and so forth - and to add THAT as a subview of the Scenario's View?

Upvotes: 1

Views: 299

Answers (1)

ColdLogic
ColdLogic

Reputation: 7275

For this, as long as I interpreted your question correctly, what you're saying is that you have 3 objects that share some minor common functionality. What I would do is abstract the common functionality into a base class, then have the 3 other classes inherit from the base class.

So you have ScenarioBase which has the common functionality like the timer property, a startTimer function, a stopTimer function, a countdown function, a startingTime variable, then probably a delegate protocol for completedScenario (this calls dismiss on your modal view controller).

@protocol CompletedScenarioDelegate : NSObject
-(void)scenarioIsComplete;
@end

@interface ScenarioBase : UIViewController <CompletedScenarioDelegate> {
     NSTimer *timer;

     int startingTime;
}

@property (nonatomic, retain) NSTimer *timer;
@property (nonatomic, readwrite) int startingTime;

-(void)startTimer;
-(void)stopTimer;
-(void)countdown;

@end

Now you have 3 scenarios you want to implement.

A class definition would probably look something like

@interface Scenario1 : ScenarioBase {
     UIImageView *puzzleBackground; //I don't really know what you want as members
                                   //Lets say this is a custom background for the puzzle
}

@end

You then implement the code for your custom puzzle, set its startingTime in it's init, call its inherited startTimer function and there you go. You can implement default functionality in the parent class for scenarioIsComplete, probably something like

-(void)scenarioIsComplete {
     UIViewController *vc = [(ScenarioAppDelegate)([[UIApplication sharedApplication] delegate]) viewController];

     [vc dismissModalViewController];

}

and that will handle the dismissal for all the children scenarios.

Upvotes: 2

Related Questions