我有一个“NSTimer”,只要我切换到不同的视图控制器,我就可以运行它。我创建了一个单例类,其中包含此代码。先是.H,然后是.M
// ApplicationManager.h
// License
//
// Created by Connor Gosell on 7/31/13.
// Copyright (c) 2013 Connor Gosell. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface ApplicationManager : NSObject{
NSTimer *ticker;
}
+(ApplicationManager*) instance;
@end
现在 .M 文件
// ApplicationManager.m
// License
//
// Created by Connor Gosell on 7/31/13.
// Copyright (c) 2013 Connor Gosell. All rights reserved.
//
#import "ApplicationManager.h"
@implementation ApplicationManager
static ApplicationManager* appMgr = nil;
+(ApplicationManager*) instance
{
@synchronized([ApplicationManager class])
{
if(!appMgr)
{
appMgr = [[self alloc] init];
}
return appMgr;
}
}
return nil;
//}
+(id) alloc
{
@synchronized([ApplicationManager class])
{
NSAssert((appMgr == nil), @"Only one instance of singleton class may be instantiated.");
appMgr = [super alloc];
return appMgr;
}
}
-(id) init
{
if(!(self = [super init]))
{
[self release];
return nil;
}
return self;
}
现在是 View Controller.H 文件
// ViewController.h
// License
//
// Created by Connor Gosell on 7/2/13.
// Copyright (c) 2013 Connor Gosell. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "ApplicationManager.h"
@interface ViewController : UIViewController{
IBOutlet UILabel *time;
NSTimer *ticker;
}
- (IBAction)start;
- (IBAction)reset;
- (void)showActivity;
@end
现在 ViewController.M 文件
// ViewController.m
// License
//
// Created by Connor Gosell on 7/2/13.
// Copyright (c) 2013 Connor Gosell. All rights reserved.
//
#import "ViewController.h"
#import "ApplicationManager.h"
@interface ViewController ()
@end
@implementation ViewController
-(IBAction) start
{
[[ApplicationManager instance]ticker ]:[NSTimer scheduledTimerWithTimeInterval:1.0 target:[ApplicationManager Class]selector:@selector(showActivity) userInfo:nil repeats:YES];
}
-(IBAction)reset
{
[[ApplicationManager instance]ticker: invalidate];
time.text = @" 0:00";
}
-(void) showActivity
{
int currentTime = [time.text intValue];
int newTime = currentTime + 1;
time.text = [NSString stringWithFormat:@"%d", newTime];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
/*-(IBAction) start
{
[[ApplicationManager instance] setTicker:[NSTimer scheduledTimerWithTimeInterval:1.0 target:self ``selector:@selector(showActivity) userInfo:nil repeats:YES]];
}
-(IBAction) reset
{
[[[ApplicationManager instance] ticker] invalidate];
time.text = @" 0:00";
}
-(void) showActivity
{
int currentTime = [time.text intValue];
int newTime = currentTime + 1;
time.text = [NSString stringWithFormat:@"%d", newTime];
}
*/
我的问题是,当我尝试在我的 iphone 上运行该应用程序时,它会返回错误:没有可见的@interface 声明选择器“ticker”并且:使用未声明的标识符“无效”这两个错误都发生在IBActions 启动和重置上的 Viewcontroller.M 文件我似乎无法找到这些错误的解决方案。任何解决方案?