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.