1

I have created a timer that is fired after 10.0 seconds and calls targetMethod: on self, which is a pointer to the NSTimer instance.This timer should only work when its associated viewcontroller screen is in the view.I have stopped the timer while loading .xib file called PhotoView.xib because timer blocks the UI. But I don't know how to start the timer again when PhotoView.xib is removed/dismissed from the Viewcontroller. Can someone please tell me how to restart the timer(of parentViewcontroller) when .xib is dismissed from parentViewcontroller?

SampleView.h

#import <UIKit/UIKit.h>

@interface SampleView : UIView

@property(nonatomic,retain)IBOutlet UIView* view;
@property(nonatomic,retain)IBOutlet UIImageView* imageview;
@property(nonatomic,retain)IBOutlet UIButton* dismissButton;

@end

SampleView.m

#import "GroupPhotoView.h"

@implementation SampleView
@synthesize imageview,view,dismissButton;

-(IBAction) dismissView: (id) sender{
    [self removeFromSuperview];
}

@end

SampleViewController.h

#import <UIKit/UIKit.h>

@interface SampleViewController : UIViewController
@property(nonatomic,retain)NSTimer *timer;

@end

SampleViewController.m

#import "SampleViewController.h"

@interface SampleViewController ()
@end

@implementation SampleViewController
@synthesize timer;


- (void)viewDidLoad {
    [super viewDidLoad];
}

-(void)viewDidAppear:(BOOL)animated{

self.timer=[NSTimer scheduledTimerWithTimeInterval:10 target:self  selector:@selector(sampleMethod:) userInfo:nil repeats:YES];
}

-(void)viewWillDisappear:(BOOL)animated{
    //stop the timer when SampleVC is not in the view
    if ([self.timer isValid])
    {
        [self.timer invalidate];
        self.timer=nil;
    }
}

- (IBAction)displayAction:(id)sender
{
    [self loadSampleImage];
}


-(void)loadSampleImage:(UIImage*)aImage{

    if ([self.timer isValid])
    {
        [self.timer invalidate];
        self.timer=nil;
    }

    SampleView *customView =(SampleView *)[[[NSBundle mainBundle]   loadNibNamed:@"PhotoView" owner:self options:nil] objectAtIndex:0];
    customView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.8];
    customView.imageview.image=[UIImage imageNamed:@"cs"];

    [customView setFrame:self.view.frame];
    [self.navigationController.view addSubview:customView];
 }
}

@end

Thanks in advance.

sarita
  • 236
  • 1
  • 11

1 Answers1

1

You can use Delegate to restart NSTimer again. A delegate allows one object to send messages to another object when an event happens. A delegate is use for similar problem when object A calls to object B but once action completed A should know that B has completed the task and take necessary action.

Create SampleView :

// Delegate to respond back in SampleView.m
id <SampleView> _delegate; 
// OR in SampleView.h
@property (strong, nonatomic) id _delegate;

After that add action completed call in SampleView.m :

-(IBAction) dismissView: (id) sender{
    [self removeFromSuperview];
    [_delegate startTimer];
}

Add method start Timer in SampleViewController.m:

customView.delegate = self;

-(void)startTimer
{
    self.timer=[NSTimer scheduledTimerWithTimeInterval:10 target:self  selector:@selector(sampleMethod:) userInfo:nil repeats:YES];
}

It will start timer again after .xib is dismissed from parentViewcontroller.

Sid Mhatre
  • 3,272
  • 1
  • 19
  • 38