У меня есть «NSTimer», который я хочу, чтобы иметь возможность запускать каждый раз, когда я переключаюсь на другой контроллер представления. У меня есть одноэлементный класс, созданный, и в нем есть этот код. .Н первым затем .МСоздание «NSTimer» в одноэлементном классе
// 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
Теперь .М Файл
// 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;
}
Теперь The 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 не декларирует селектор «тикер» и: использование необъявленной идентификатора «аннулирует» обе эти ошибки происходят в файле Viewcontroller.M при запуске и сбросе IBActions Я не могу найти решение этих ошибок. любые решения?
У вас много кода, но, похоже, у вас нет вопросов. – Caleb
@ Калеб добавил дополнительную информацию. просматривал xcode, чтобы найти его! спасибо за головы – Gosell1
'[[экземпляр ApplicationManager] тикер: invalidate];' как вы думаете, что этот двоеточие означает? –