Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Google Sign In with OpenID
Please do not post the same thing multiple times. The board software automatically flags certain posts as needing moderator attention. This happens the most often for new users. I'm pretty sure this is made clear at the time you attempt to post. Posting the same thing over and over again just makes that many more posts the moderators have to weed through later. This makes us sad. Don't make us sad. If your post/thread doesn't appear, just wait a while. Don't post it again. If it hasn't shown up by the next day, then you can try again. I normally go through posts in the mornings, and try to check a few times throughout the day, but I'm not here 24/7. There will typically be a significant delay before posts are approved. Just be patient.

Stop timer from UIVIEWSUBCLASS

alok14ecc@gmail.comalok14ecc@gmail.com Posts: 5New Users
edited August 2012 in iPhone SDK Development
I have start a timer to call a method after 10 seconds. the timer start from didFinishLaunchingWithOptions. it works fine but I want to stop that timer from another UISubclass at the time of logout. How I can do this my code is
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[NSThread sleepForTimeInterval:2.0];

[self.window addSubview:viewController.view];
[self setupTimer];
[self.window makeKeyAndVisible];

return YES;
}
-(void)setupTimer;
{
timer = [NSTimer timerWithTimeInterval:10
target:self
selector:@selector(triggerTimer:)
userInfo:nil
repeats:YES];
[[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
}

-(void)triggerTimer:(NSTimer*)timer;
{
Getlocation *object=[[Getlocation alloc] init];
[object updatelocation];
}
-(void)stopTimer:(NSTimer*)timer;
{
[timer invalidate];
timer=nil;
}
Post edited by alok14ecc@gmail.com on

Replies

  • Duncan CDuncan C Posts: 8,150Tutorial Authors, Registered Users
    First, don't ever use sleep or sleepForTimeInterval. That blocks your app, and the system is likely to terminate you for being unresponsive. Instead, factor your code like this:
    - (void) finishLaunching
    {
    [self.window addSubview:viewController.view];
    [self setupTimer];
    [self.window makeKeyAndVisible];
    }

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
    [self performSelector: @selector(finishLaunching) withObject: nil afterDelay: 2.0];
    return YES;

    return YES;
    }
    Second, if you want to be able to stop the timer from another class, add a stopTimer method to your app delegate. Make your app delegate a custom class like MyDelegate and make the stopTimer method a public method.

    Then from anywhere in your program, do this:
    MyDelegate *delegate = (MyDelegate *)[UIApplication sharedApplication].delegate;
    [delegate stopTimer];
    Regards,

    Duncan C
    WareTo

    mug

    Animated GIF created with Face Dancer, available for free in the app store.
    alok14ecc@gmail.com
Sign In or Register to comment.