如何在 ios 9 中关闭 UIView.xib 时在 UIViewController 中重新启动预定的 NSTimer?
How to restart the scheduled NSTimer in UIViewController when UIView.xib is dismissed in ios 9?
我创建了一个计时器,它在 10.0 秒后触发并调用 targetMethod: on self,它是一个指向 NSTimer instance.This 计时器的指针,它应该只在其关联的 viewcontroller 屏幕打开时工作view.I 在加载名为 PhotoView.xib 的 .xib 文件时停止了计时器,因为计时器阻塞了 UI。但是当 PhotoView.xib 从 Viewcontroller 变为 removed/dismissed 时,我不知道如何再次启动计时器。
有人能告诉我当 .xib 从父 Viewcontroller 中解雇时如何重新启动计时器(父 Viewcontroller 的)吗?
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
提前致谢。
您可以使用Delegate再次重启NSTimer。委托允许一个对象在事件发生时向另一个对象发送消息。当对象 A 调用对象 B 时,委托用于类似的问题,但是一旦操作完成,A 应该知道 B 已完成任务并采取必要的操作。
创建示例视图:
// Delegate to respond back in SampleView.m
id <SampleView> _delegate;
// OR in SampleView.h
@property (strong, nonatomic) id _delegate;
在 SampleView.m 中添加操作完成后调用:
-(IBAction) dismissView: (id) sender{
[self removeFromSuperview];
[_delegate startTimer];
}
在SampleViewController.m中添加方法启动定时器:
customView.delegate = self;
-(void)startTimer
{
self.timer=[NSTimer scheduledTimerWithTimeInterval:10 target:self selector:@selector(sampleMethod:) userInfo:nil repeats:YES];
}
.xib 从 parentViewcontroller 中退出后,它将再次启动计时器。
我创建了一个计时器,它在 10.0 秒后触发并调用 targetMethod: on self,它是一个指向 NSTimer instance.This 计时器的指针,它应该只在其关联的 viewcontroller 屏幕打开时工作view.I 在加载名为 PhotoView.xib 的 .xib 文件时停止了计时器,因为计时器阻塞了 UI。但是当 PhotoView.xib 从 Viewcontroller 变为 removed/dismissed 时,我不知道如何再次启动计时器。 有人能告诉我当 .xib 从父 Viewcontroller 中解雇时如何重新启动计时器(父 Viewcontroller 的)吗?
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
提前致谢。
您可以使用Delegate再次重启NSTimer。委托允许一个对象在事件发生时向另一个对象发送消息。当对象 A 调用对象 B 时,委托用于类似的问题,但是一旦操作完成,A 应该知道 B 已完成任务并采取必要的操作。
创建示例视图:
// Delegate to respond back in SampleView.m
id <SampleView> _delegate;
// OR in SampleView.h
@property (strong, nonatomic) id _delegate;
在 SampleView.m 中添加操作完成后调用:
-(IBAction) dismissView: (id) sender{
[self removeFromSuperview];
[_delegate startTimer];
}
在SampleViewController.m中添加方法启动定时器:
customView.delegate = self;
-(void)startTimer
{
self.timer=[NSTimer scheduledTimerWithTimeInterval:10 target:self selector:@selector(sampleMethod:) userInfo:nil repeats:YES];
}
.xib 从 parentViewcontroller 中退出后,它将再次启动计时器。