【问题标题】:NSTimer with error message?带有错误消息的 NSTimer?
【发布时间】:2013-08-26 08:41:25
【问题描述】:

我正在尝试实现从 5 秒开始倒计时的 NSTimer。但是我收到消息“没有已知的选择器 scheduleTimerWithTimeInterval:target:selector:userInfo:repeats 的类方法......”我错过了什么?如果您在方法 -void(showInView) 中查看 MTPopupWindow.m 的底部,您会发现出现错误的代码。

问候

MTPopupWindow.h

@class MTPopupWindow;

@protocol MTPopupWindowDelegate <NSObject>
@optional
- (void) willShowMTPopupWindow:(MTPopupWindow*)sender;
- (void) didShowMTPopupWindow:(MTPopupWindow*)sender;
- (void) willCloseMTPopupWindow:(MTPopupWindow*)sender;
- (void) didCloseMTPopupWindow:(MTPopupWindow*)sender;
@end

@interface MTPopupWindow : UIView

+(MTPopupWindow*)showWindowWithHTMLFile:(NSString*)fileName;
+(MTPopupWindow*)showWindowWithHTMLFile:(NSString*)fileName insideView:(UIView*)view;
- (void)timerFireMethod; 
-(void)showInView:(UIView*)v;
+(void)setWindowMargin:(CGSize)margin;

@property (strong, nonatomic) NSString* fileName;
@property (strong, nonatomic) UIWebView* webView;
@property (weak, nonatomic) id <MTPopupWindowDelegate> delegate;
@property (nonatomic) BOOL usesSafari;
@property (nonatomic, retain) NSTimer* timer;
@end

MTPopupWindow.m

#import "MTPopupWindow.h"
#import "QuartzCore/QuartzCore.h"
#define kCloseBtnDiameter 30
#define kDefaultMargin 18
static CGSize kWindowMarginSize;

@interface MTPopupWindow() <UIWebViewDelegate>
{
UIView* _dimView;
UIView* _bgView;
UIActivityIndicatorView* _loader;
NSTimer *timer;
}
@end

@interface MTPopupWindowCloseButton : UIButton
+ (id)buttonInView:(UIView*)v;
@end

@interface UIView(MTPopupWindowLayoutShortcuts)
-(void)replaceConstraint:(NSLayoutConstraint*)c;
-(void)layoutCenterInView:(UIView*)v;
-(void)layoutInView:(UIView*)v setSize:(CGSize)s;
-(void)layoutMaximizeInView:(UIView*)v withInset:(float)inset;
-(void)layoutMaximizeInView:(UIView*)v withInsetSize:(CGSize)insetSize;
@end

@implementation MTPopupWindow

@synthesize fileName = _fileName;
@synthesize webView = _webView;
@synthesize usesSafari = _usesSafari;
@synthesize delegate = _delegate;
@synthesize timer;

-(void)showInView:(UIView*)v
{
.......
self.timer = [NSTimer scheduledTimerWithTimeInterval:5 taget:self selector:@selector(timerFireMethod:) userInfo:nil repeats:NO];  

}

-(void)timerFireMethod:(NSTimer *)theTimer{

NSLog(@"bla bla time is out");
MTPopupWindowCloseButton* btnClose = [MTPopupWindowCloseButton buttonInView:self];
[btnClose addTarget:self action:@selector(closePopupWindow) forControlEvents:UIControlEventTouchUpInside];

}

【问题讨论】:

  • 如果你只想运行一次,那么你应该使用 [self performSelector:@selector(YourMethod) withObject:nil afterDelay:5];

标签: ios nstimer nstimeinterval


【解决方案1】:

您在方法调用中将“target”拼错为“taget”。做:

self.timer = [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(timerFireMethod:) userInfo:nil repeats:NO];

相反。

【讨论】:

  • 干杯!最小的错误往往是最严重的错误!
【解决方案2】:

.h 文件中的方法是: - (void)timerFireMethod; 但在选择器处是: timerFireMethod: 以“:”结尾但没有参数,就是这个原因。

您在 h 文件和 m 文件之间的定义不一致。 请更正 timerFireMethod 方法。

【讨论】:

  • 这可能会导致不完整的实现警告,但不会导致此特定错误。指定给计时器的选择器与实现的选择器相同,因此它也不应该在运行时导致崩溃。
  • 您看到错误信息了吗? "没有已知的选择器类方法
猜你喜欢
  • 2013-01-05
  • 2013-02-06
  • 2014-11-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-01
  • 2017-11-13
相关资源
最近更新 更多