在单例类中创建“NSTimer”

时间:2013-08-05 22:06:41

标签: ios objective-c

我有一个'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;
}

现在查看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' 和:使用未声明的标识符'invalidate' 这两个错误都发生在IBActions启动和重置的Viewcontroller.M文件中 我似乎无法找到这些错误的解决方案。任何解决方案?

1 个答案:

答案 0 :(得分:3)

  

没有可见的@interface声明选择器'ticker'

您正在尝试调用不存在的方法:

[[ApplicationManager instance]ticker ]

这相当于:

[[ApplicationManager instance] ticker]

您是否在ApplicationManager.h中看到名为-ticker的方法?我不。也许你打算将ivar宣布为财产或什么?该线的其余部分非常混乱,我不确定你要做什么。

  

使用未声明的标识符'invalidate'

[[ApplicationManager instance]ticker: invalidate]

这条线真的没有任何意义。我认为你意味着之类的东西:

[[ApplicationManager instance].ticker invalidate];

但这不起作用,因为如上所述,当前在ApplicationManager.h中没有声明ticker属性或方法。

另外,为什么你首先要创建这个单例?你所拥有的只有一个计时器,而计时器不是你需要从许多地方访问的东西(不是那个单身人士的理由)。摆脱单例baloney,在视图控制器中实例化计时器,并对更简单,更易于维护的代码感到满意。