1

我正在尝试使用 Xcode 4.3.1 为 Iphone 编写一个应用程序,我想做的是按下一个按钮并触摸触摸屏上的某处并显示一个标签(附有计时器)并计数从某个数字下降。当计时器到达 0:00 时,我希望它自己失效,为此我需要保留它的参考。

我不知道我将预先使用多少个标签/计时器,所以我想我会使用一个数组来存储每个。我对Objective-C语言知之甚少。到目前为止,我所做的一切都只是复制了我在其他 stackoverflow 问题中看到的示例,试图理解它们。到目前为止,我已经能够构建一个相对实用的计时器。

以下是我当前的计时器代码。目前,它只是一个连接到预制标签的预制按钮,具有我希望我的计时器具有的所有功能。它从 5 分钟开始,将自身格式化为分钟:秒,并在到达 0:00 时使计时器失效。定时器启动后,该按钮还可用作停止/复位功能。

视图控制器.h

@interface ViewController : UIViewController{

    IBOutlet UILabel *timerDisplay;
    NSTimer *timer;
    bool timerActive;
    int MainInt;
    int minutes;
    int seconds;

}

@property (nonatomic, retain) UILabel *timerDisplay;

-(IBAction)start:(id)sender;
-(void)countdown;
-(void)timerStop;
-(void)timeFormat;

@end

视图控制器.m

@implementation ViewController

@synthesize timerDisplay;

-(void)timeFormat{
    seconds = MainInt % 60;
    minutes = (MainInt - seconds) / 60;
    timerDisplay.text = [NSString stringWithFormat:@"%d:%.2d", minutes, seconds];
}

-(void)countdown {
    MainInt -= 1;
    [self timeFormat];
    if (MainInt <=0){
        timerActive = NO;
        [self->timer invalidate];
        self->timer = nil;
    }
}

-(IBAction)start:(id)sender {
    MainInt = 300;
    [self timeFormat];
    if(timerActive == NO){
        timerActive = YES;
        self->timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(countdown) userInfo:nil repeats:YES];
    }
    else {
        timerActive = NO;
        [self->timer invalidate];
        self->timer = nil;
    }
}

任何帮助将不胜感激。我最希望能够使用数组或其他东西一次拥有多个计时器的帮助。硬编码大量计时器将是一件愚蠢的事情。

干杯。

编辑2:

我有下面的代码作为我的应用程序,这几乎正是我想要它做的。除了在这段代码中,我一次只能运行一个计时器,这是我的问题。

在这段代码中,我存储了触摸屏上最近一次点击的值(使用它的坐标将计时器放置在屏幕上的某个位置),以及一个按钮,当我按下它时,一个正在运行的计时器(带有 UILabel)将出现在之前存储的拍子的位置。它将一直运行直到完成,然后使自身失效并将自身从视图中移除。这工作正常。

但是,当我在原始计时器完成之前再次按下按钮时,它只会在新位置创建一个新计时器(使用 UILabel)。这个新计时器可以正常工作,但旧计时器已丢失其计时器参考,因此无法完成,我无法删除它。

视图控制器.h:

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController{

    UILabel *timerDisplay;
    NSTimer *timer;
    CGPoint startPoint;
    int MainInt;

}

@property CGPoint startPoint;

-(IBAction)startTimer:(id)sender;
-(void)countdown;
-(void)timeFormat;
-(void)start;

@end

视图控制器.m:

@implementation ViewController

@synthesize startPoint;

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    UITouch *theTouch = [touches anyObject];
    startPoint = [theTouch locationInView:self.view];
}

-(IBAction)startTimer:(id)sender {

    timerDisplay = [[UILabel alloc] initWithFrame:CGRectMake(startPoint.x -15, startPoint.y - 15, 50, 50)];
    timerDisplay.textAlignment = UITextAlignmentCenter;
    timerDisplay.text = [NSString stringWithFormat:@"%i", MainInt];
    timerDisplay.backgroundColor = [UIColor greenColor];
    timerDisplay.textColor = [UIColor whiteColor];
    [self.view addSubview:timerDisplay];
    [self start];

}

-(void)timeFormat{
    int seconds = MainInt % 60;
    int minutes = (MainInt - seconds) / 60;
    timerDisplay.text = [NSString stringWithFormat:@"%d:%.2d", minutes, seconds];
}

-(void)countdown {
    MainInt -= 1;
    [self timeFormat];
    if (MainInt <= 0){
        [self->timer invalidate];
        self->timer = nil;
        [timerDisplay removeFromSuperview];
    }
}

-(void)start {
    MainInt = 5;
    [self timeFormat];
    if(self->timer == nil){
        self->timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(countdown) userInfo:nil repeats:YES];
    }
}

@end

我希望有人可以帮助我创建一个新类来保存每个新的计时器实例并保留对它们的引用。我缺乏目标 C 的知识来完成这项工作。或者,也许您可​​以将我链接到可以帮助我的示例。

干杯。

4

1 回答 1

1

你的代码对我来说看起来不错。我会说你有多余的实例变量。timerActive是多余的,因为您可以检查是否self->timer是非nil. seconds并且minutes可能只是本地的-timeFormat,它们不需要是实例变量。

问题是您不知道如何将其推广到具有多个计时器的多个按钮标签对吗?我建议您创建一个新类来充当每个按钮标签对的控制器。该类将派生自NSObject. 您的视图控制器类的当前实例变量将是这个新类的实例变量,尽管您不一定要IBOutlet用于标签。(一个插座连接在一个 NIB 中,但这些可能是动态创建的,并且会在代码中连接。)

您将为所需的每个按钮标签对分配并初始化这个新类的实例。您将传递指向标签的指针和开始时间值。您还可以将指针传递给ViewController拥有它的对象,以便它可以在停止或类似的情况下通知它。相应的按钮将设置为针对这个新控制器。ViewController将跟踪可变数组中的每个控制器。当它完成一个时,它会删除它的标签、它的按钮和控制器。

这有帮助吗?

于 2012-05-12T04:50:18.580 回答