Counter across 3 View Controllers?

Need a way to make a counter work across at least 3 View Controllers.

One view controller has an -(IBAction)plus1 button and an -(IBAction)plus 0 button.


Those buttons segue to the next view controller, that has an -(IBAction)plus2 button and an -(IBAction)plus 0 button.... etc.


Each segues and adds all button clicks through to the last view controller with an IBOutlet UILabel *count text label.


I got this to work on one view controller. Can't figure out how to make it work across 3VCs. Someone suggested Singleton?

Thanks for any thoughts.


Thanks JDL55

Posted on Feb 2, 2013 9:11 PM

Reply
34 replies

Sep 21, 2013 1:23 PM in response to Llessur999

HI, cool thanks, you're right, That was my TEST code project. I wish I knew as much about coding.

I thought I clicked on ARC. Is there a way to tell me if I have arc?

Anyways, it worked on my real app. It went to 0. But when I click at the start and went to the finish, it is still adding the original. I'm doing something wrong.

User uploaded file

It is adding all the previous numbers. I'm using a singleton method.

Do you think that is ignoring my iBOutlet?

User uploaded file

Thanks Jeff

Sep 26, 2013 10:22 PM in response to JDL55

Hi, So I'm using...

- (IBAction)clearButtonClick:(id)sender {


self.currentValueLabel.text = @"";


...which is great and resets the label. But when you click on the Home Tab to start over, the tally is not cleared

and continues to add all the other VCs to the Label. Of course if I was allowed to end the app, not a problem

so I need a clear tally. Is this possible?

Sep 27, 2013 11:12 PM in response to JDL55

Anyone know the fix for this?

If I click the "clearLabel" button on the last VC of a segued counter before I press the Home button to start a new count, shouldn't the value be "0"?

Well, it's not. Even though it is cleared, it adds the new game counter numbers to the old tally.

Then I tried a new approach. I put a segue from the "clearValue" button on the 5th VC to the 1st VC.

The VCs start to add up the clicks but the last VC again adds the new game counter numbers to the old tally.

How would you clear the tally so it adds up only the new game counter when clicking back to the Home Tab or even segueing to Screen 1?


Thanks

Sep 28, 2013 7:20 AM in response to JDL55

Maybe change something in the AppDelegate since I'm clicking on Home button?


- (void)applicationWillEnterForeground:(UIApplication *)application

{

// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.

}

I don't know just a thought...

Sep 28, 2013 10:25 AM in response to JDL55

Remember we don't know the structure of your application. In the posts above you mention a Home button and Home tab, so it is unclear what you mean by pressing Home. Lots of design patterns would work. Here is an one easy example. I recommend you build a simple test project like this and play around with it to see how it works, then decide how to revise your real project.


Assume: Storyboard with push segues between view controllers. Each view controller shows the current value in a text field, and optionally has buttons Plus 0, Plus 1, and Restart. All segues use identifier ShowNextStep. The segues are from VC to VC, not from button to VC, because they will be programmatically invoked.


Navigation Controller > Step 1 View Controller > Step 2 View Controller > Step 3 View Controller



[AppDelegate.h]

@propertyNSInteger currentValue; // persist currentValue on the application delegate.


[AppDelegate.m]

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

{

// initialize on application launch

self.currentValue = 0;

returnYES;

}



All three view controllers use the same class StepViewController. This allows you to add an arbitrary number of view controllers to the storyboard without creating new classes or writing any code.


[StepViewController.h]

#import <UIKit/UIKit.h>


@interface StepViewController : UIViewController


@property (weak, nonatomic) IBOutletUITextField *currentValueField;


// actions don't need to be hooked up on every view controller

- (IBAction)plus0ButtonClick:(id)sender;

- (IBAction)plus1ButtonClick:(id)sender;

- (IBAction)restartButtonClick:(id)sender;


@end


[StepViewController.m]

#import "StepViewController.h"

#import "AppDelegate.h"


@interfaceStepViewController ()


@end


@implementation StepViewController


- (void)viewDidLoad

{

[superviewDidLoad];

// show currentValue

NSInteger currentValue =((AppDelegate *)([UIApplication sharedApplication].delegate)).currentValue;

self.currentValueField.text = [NSString stringWithFormat:@"%d", currentValue];

}


- (IBAction)plus0ButtonClick:(id)sender {

// no value change, navigate to next step

[selfperformSegueWithIdentifier:@"ShowNextStep"sender:self];

}


- (IBAction)plus1ButtonClick:(id)sender {

// change value, navigate to next step

NSInteger currentValue = ((AppDelegate *)([UIApplication sharedApplication].delegate)).currentValue;

((AppDelegate *)([UIApplication sharedApplication].delegate)).currentValue = currentValue + 1;

[selfperformSegueWithIdentifier:@"ShowNextStep"sender:self];

}


- (IBAction)restartButtonClick:(id)sender {

// reset value, navigate to first step (won't work if modal segue)

((AppDelegate *)([UIApplicationsharedApplication].delegate)).currentValue = 0;

[self.navigationControllerpopToRootViewControllerAnimated:YES];

}


@end



If want to totally control navigation with your buttons, hide the navigation bar.

self.navigationController.navigationBarHidden = YES;


If you want currentValue to refresh on the root view controller (Step 1), add a handler for viewWillAppear. The root view controller is not reloaded unless iOS terminates the app so viewDidLoad won't work. I think you want the root view controller to always reset to the initial value, so you probably don't need to do this.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Counter across 3 View Controllers?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.