Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 783631
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T20:34:43+00:00 2026-05-14T20:34:43+00:00

Right, I’m trying to make an app that has a calculation that involves a

  • 0

Right, I’m trying to make an app that has a calculation that involves a stopwatch. When a button on the calculation view is clicked a stopwatch slides in from the bottom. This all works fine, the problem I can’t get my head around is how to send the recorded time back to the previous controller to update a textfield.

I’ve simplified the code and stripped out most irrelevant stuff.

Many thanks.

CalculationViewController.h

#import <UIKit/UIKit.h>

@interface CalculationViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> {

IBOutlet UITextField *inputTxt;
}
@property (nonatomic, retain) UITextField *inputTxt;

- (IBAction)showTimer:(id)sender;
@end

CalculationViewController.m

#import "CalculationViewController.h"
#import "TimerViewController.h"

@implementation CalculationViewController

- (IBAction)showTimer:(id)sender {

   TimerViewController *timerView = [[TimerViewController alloc] init];
   [self.navigationController presentModalViewController:timerView animated:YES];
}

TimerViewController.h

#import <UIKit/UIKit.h>

@interface TimerViewController : UIViewController {

IBOutlet UILabel *time;
NSTimer *myTicker;
}

- (IBAction)start;
- (IBAction)stop;
- (IBAction)reset;
- (void)showActivity;
@end

TimerViewController.m

#import "TimerViewController.h"
#import "CalculationViewController.h"

@implementation TimerViewController

- (IBAction)start { 
myTicker = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(showActivity) userInfo:nil repeats:YES];
}

- (IBAction)stop {
[myTicker invalidate];

#Update inputTxt on calculation view here

[self dismissModalViewControllerAnimated:YES];
}

- (IBAction)reset {
time.text = @"0";
}

- (void)showActivity {
int currentTime = [time.text intValue];
int newTime = currentTime + 1;

time.text = [NSString stringWithFormat:@"%d", newTime];
}
@end
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T20:34:44+00:00Added an answer on May 14, 2026 at 8:34 pm

    A couple of ways to do this are:

    • use NSNotificationCenter, or
    • make CalculationViewController a delegate of TimerViewController (define a protocol that CalculationViewController implements)

    In both cases, the TimerViewController would notify CalculationViewController it is done and at the same time pass the data with it. So in the stop method, it would do postNotificationName:object:userInfo or call a delegate method. Then the dismiss would instead be done in CalculationViewController when it receives the notification.

    EDIT:
    There isn’t necessarily one right way every time. Depending on the size and complexity of the app and exact requirements of the situation, some ways are better than others. The app delegate is ok for sharing data across view controllers but it’s probably better to use notifications or delegates to signal events across controllers like you want to do here.

    A protocol is a stricter and more self-documenting and self-contained approach than notifications but for this simple case, either one is fine.

    Here’s how you can implement it using a protocol/delegate approach:

    TimerViewController.h:

    @protocol TimerViewDelegate
    -(void)timerStopped:(NSString *)timerData;
    @end
    @interface TimerViewController : UIViewController {
        //other ivars...
        id<TimerViewDelegate> delegate;
    }
    //other properties...
    @property (nonatomic, assign) id <TimerViewDelegate> delegate;
    //method declarations...
    @end
    

    TimerViewController.m:

    @implementation TimerViewController
    @synthesize delegate;
    - (IBAction)stop {
        [myTicker invalidate];
        NSString *timerData = @"timer data here";
        [self.delegate timerStopped:timerData];
    }
    @end
    

    CalculationViewController.h:

    #import "TimerViewController.h"
    @interface CalculationViewController : UIViewController 
      <UITableViewDelegate, UITableViewDataSource, TimerViewDelegate > {
        ...
    }
    @end
    

    CalculationViewController.m:

    - (IBAction)showTimer:(id)sender {
        TimerViewController *timerView = [[TimerViewController alloc] init];
        timerView.delegate = self;
        [self.navigationController presentModalViewController:timerView animated:YES];
        [timerView release];
    }
    
    - (void)timerStopped:(NSString *)timerData
    {
        inputTxt.text = timerData;
        [self dismissModalViewControllerAnimated:YES];  
    }
    

    And here’s the notification version:

    CalculationViewController.m:

    - (IBAction)showTimer:(id)sender {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(timerStopped:) name:@"timerStopped" object:nil];
        TimerViewController *timerView = [[TimerViewController alloc] init];
        [self.navigationController presentModalViewController:timerView animated:YES];
        [timerView release];
    }
    - (void)timerStopped:(NSNotification*)notification
    {
        NSString *timerData = [[notification userInfo] objectForKey:@"timerData"];
        inputTxt.text = timerData;
        [self dismissModalViewControllerAnimated:YES];
    }
    

    TimerViewController.m:

    - (IBAction)stop {
        [myTicker invalidate];
        NSDictionary *dict = [NSDictionary dictionaryWithObject:@"timer data here" forKey:@"timerData"];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"timerStopped" object:self userInfo:dict];
    }
    

    In this very simple case, a notification looks ok to use. In either case, neither controller has to know the inner details of each other nor do they depend on a third party like the app delegate. They only have to agree on the notification name and userinfo keys. The protocol approach is even more strict but self-documenting.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 403k
  • Answers 403k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer This is basically the strategy that Teradata has used. You… May 15, 2026 at 5:15 am
  • Editorial Team
    Editorial Team added an answer You can use option of :id and :class <% content_tag_for(:ul,… May 15, 2026 at 5:15 am
  • Editorial Team
    Editorial Team added an answer As mentioned, NSValue can wrap a plain struct using +value:withObjCType:… May 15, 2026 at 5:15 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.