-3

我有一个“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 文件我似乎无法找到这些错误的解决方案。任何解决方案?

4

1 回答 1

3

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

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

[[ApplicationManager instance]ticker ]

这相当于:

[[ApplicationManager instance] ticker]

您是否看到在 ApplicationManager.h 中命名的方法-ticker?我不。也许您打算将该 ivar 声明为属性或其他东西?该行的其余部分非常混乱,我不确定您要做什么。

使用未声明的标识符“无效”

[[ApplicationManager instance]ticker: invalidate]

那条线真的没有任何意义。我认为您的意思是:

[[ApplicationManager instance].ticker invalidate];

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

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

于 2013-08-05T22:27:45.647 回答