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 5849189
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T12:57:52+00:00 2026-05-22T12:57:52+00:00

EDIT: Added the NSRunLoop from my code, as mentioned by Deepak below. This was

  • 0

EDIT: Added the NSRunLoop from my code, as mentioned by Deepak below. This was originally in my code and forgot to add as it was commented out.

I have 2 classes: MainViewController, and ConfigViewController. The user switches to the ConfigView and uses a UIDatePicker / UIButton combo to set a Date/Time. Upon grabbing the correct time from the UIDatePicker object, I setup a NSTimer to fire as per the following code:

ConfigViewController.m

-(IBAction)setAlarmDate:(id)sender {
//Instantiate to get access to doAlarm:
MainViewController *mvc = [[MainViewController alloc] init];

dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateStyle:NSDateFormatterMediumStyle];
[dateFormat setTimeStyle:NSDateFormatterShortStyle];
NSString *target = [NSString stringWithFormat:@"%@",[dateFormat stringFromDate:datePicker.date]];

alarmDate = [datePicker date];

mvc.fireTimer = [[NSTimer alloc] 
                  initWithFireDate:alarmDate interval:1 target:mvc
                  selector:@selector(doAlarm:) userInfo:nil repeats:YES];

NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
[runLoop addTimer:mvc.timer forMode:NSDefaultRunLoopMode];


[self dismissModalViewControllerAnimated:NO];
}

The doAlarm: method is as follows:

MainViewController.m

- (void)doAlarm:(NSTimer *)timer {

NSLog(@"Called doAlarm:");

UIImage *ac = [UIImage imageNamed:@"alarmclock.png"];
[self.alarmview setImage:ac];
[self.alarmview setHidden:NO];
[self.view addSubview:alarmview];
[self.view bringSubviewToFront:alarmview];

}

However, when I set the alarm date, the timer fails to fire. I think the following problems are afoot:

1) I am instantiating a new instance of the MainViewController class, setting the Timer going and then passing control back to the “original” instance of MainViewController when I dismiss the MVC. At this point, probably the “new” instance of MainViewController is nothing but a dangling pointer, and is never referenced again anyway, hence no segfault.

2) doAlarm: references self.view, which is supposed to be the MainViewController.view, but as it’s instantiated in the scope of ConfigViewController, the alarm image would never be seen anyway…

I imagine my theories are a bit unfounded, but with my current level of knowledge, they make sense to me.

Any light you can shed on the above would be smashing.

Many thanks!

swisscheese.

  • 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-22T12:57:53+00:00Added an answer on May 22, 2026 at 12:57 pm

    If you go to the documentation for initWithFireDate:interval:target:selector:userinfo:repeats:, you will notice that while the timer is initialized it is not scheduled yet. You will need to add it a NSRunLoop to make it work. Do this after you have initialized an alarm as above.

    [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
    

    You can also consider using the scheduledTimerWithTimeInterval:target:selector:userInfo:repeats: which does that for you.

    There are of course few other things here.

    New MainViewController object

    You guessed it right. You are creating an unrelated object which will just leak. To solve this you can maintain a weak reference to the MainViewController object. You can do this as follows –

    @class MainViewController; // Forward declaration 
    
    @interface ConfigViewController {
        ...
        MainViewController *mainViewController;
    }
    ...
    @property (nonatomic, assign) MainViewController *mainViewController;
    ...
    @end
    

    in ConfigViewController.m:

    #import "MainViewController.h"
    
    @implementation ConfigViewController
    
    @synthesize mainViewController;
    ...
    
    - (IBAction)setAlarmDate:(id)sender {
        dateFormat = [[NSDateFormatter alloc] init];
        [dateFormat setDateStyle:NSDateFormatterMediumStyle];
        [dateFormat setTimeStyle:NSDateFormatterShortStyle];
    
        alarmDate = [datePicker date];
    
        mainViewController.fireTimer = [[NSTimer alloc] 
                      initWithFireDate:alarmDate interval:1 target:mainViewController
                      selector:@selector(doAlarm:) userInfo:nil repeats:YES];
    
        NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
        [runLoop addTimer:mainViewController.timer forMode:NSDefaultRunLoopMode];
    
        mainViewController = nil;
        [self dismissModalViewControllerAnimated:NO];
    }
    ...
    @end
    

    Timer

    I am not sure if you need a repeating timer here. There is an alternative – performSelector:withObject:afterDelay:. This will invoke a method on an object this is called after the desired delay. Does what it calls itself. It will be something like this –

    [mvcReference performSelector:@selector(doAlarm) 
                       withObject:nil 
                       afterDelay:[datePicker.date timeIntervalSinceNow]];
    

    doAlarm:

    I did not get what you wanted to know here but you are right. self is a reference to the MainViewController object. Unless you retained the view controller that you displayed modally, it would have been dismissed and deallocated by then.

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

Sidebar

Related Questions

I have to translate the following code from Java to Scala: EDIT: added if-statements
EDIT: I have added Slug column to address performance issues on specific record selection.
EDIT: Modified title and added update. UPDATE : We no longer believe this is
So I just created a textbox with JavaScript like this: EDIT: Added the len
In the following code why does mockTest.ToString() return Null? EDIT: Added comment into example
in C# I'd like to invoke the label edit of a newly added item
Edit: This question was written in 2008, which was like 3 internet ages ago.
Edit: From another question I provided an answer that has links to a lot
EDIT: This was formerly more explicitly titled: - Best solution to stop Kontiki's KHOST.EXE
EDIT: This question is more about language engineering than C++ itself. I used C++

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.