无法创建两个秒表

时间:2014-02-10 23:19:44

标签: ios iphone objective-c xcode

我正在尝试创建一个应用程序,其中一个功能是能够同时计时多个事物。我在视图上有两个UILabel和两个UIButtons,并且有代码可以在按下相应按钮时使一个标签开始计时。从我的代码中可以看出,我有两件事:

#import "ViewController.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UILabel *display;
- (IBAction)startPressed:(id)sender;

@property (weak, nonatomic) IBOutlet UILabel *display2;
- (IBAction)startPressed2:(id)sender;

@end

@implementation ViewController {

    bool start;
    bool start2;

    NSTimeInterval time;
    NSTimeInterval time2;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.display.text = @"0:00";
    self.display2.text = @"0:00";

    start = false;
    start2 = false;

}

-(void) update1 {

    if (start == false) {

        return;
    }

    NSTimeInterval currentTime = [NSDate timeIntervalSinceReferenceDate];
    NSTimeInterval elapsedTime = currentTime - time;

    int minutes = (int)(elapsedTime / 60.0);

    int seconds = (int)(elapsedTime = elapsedTime - (minutes * 60));

    self.display.text = [NSString stringWithFormat:@"%u:%02u", minutes, seconds];

    [self performSelector:@selector(update1) withObject:self afterDelay:0.1];
}

-(void) update2 {
    if (start2 == false) {

        return;
    }

    NSTimeInterval currentTime2 = [NSDate timeIntervalSinceReferenceDate];
    NSTimeInterval elapsedTime2 = currentTime2 - time2;

    int minutes2 = (int)(elapsedTime2 / 60.0);

    int seconds2 = (int)(elapsedTime2 = elapsedTime2 - (minutes2 * 60));

    self.display2.text = [NSString stringWithFormat:@"%u:%02u", minutes2, seconds2];

    [self performSelector:@selector(update2) withObject:self afterDelay:0.1];

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (IBAction)startPressed:(id)sender {

    if (start == false) {
        start = true;

        time = [NSDate timeIntervalSinceReferenceDate];

        [sender setTitle:@"Stop" forState:UIControlStateNormal];

        [self update1];
    }else {

        start = false;

        [sender setTitle:@"Start" forState:UIControlStateNormal];

    }
}
- (IBAction)startPressed2:(id)sender {
    if (start2 == false) {
       start2 = true;

        time2 = [NSDate timeIntervalSinceReferenceDate];

        [sender setTitle:@"Stop" forState:UIControlStateNormal];

        [self update2];
    }else {

        start2 = false;

        [sender setTitle:@"Start" forState:UIControlStateNormal];

    }

}

@end

但是,当我运行应用程序时,无论我按哪个按钮,第一个Label都会开始计数。请帮忙,我不能让两个计时器同时运行。

谢谢!

2 个答案:

答案 0 :(得分:2)

注意:问答代码已经大幅更新,因为这个答案是第一次写的。

updateupdate2都调用相同的方法:

[self performSelector:@selector(update) withObject:self afterDelay:0.1];

update2应致电:

[self performSelector:@selector(update2) withObject:self afterDelay:0.1];

避免这类错误的最佳方法是通过良好的命名,update1update2会避免此错误。此外,还有相当多的代码可以考虑到常见的menthols中,以确保通过一次代码更改来实现两个定时器共同的未来更改。

Objective-C使用BOOL YESNO作为布尔常量而非bool使用truefalse。它通常可以更容易地使用系统的约定。

命名非常重要,因为它消除了重复的代码。这是一个演示实现:

#import "ViewController.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UILabel *display1;
@property (weak, nonatomic) IBOutlet UILabel *display2;

@property (nonatomic) NSTimeInterval time1;
@property (nonatomic) NSTimeInterval time2;
@property (nonatomic, getter = isRunning1) BOOL running1;
@property (nonatomic, getter = isRunning2) BOOL running2;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.display1.text = [self elapsedTimeInterval:0];
    self.display2.text = [self elapsedTimeInterval:0];
    self.running1 = NO;
    self.running2 = NO;
}

-(void) update {
    if (self.isRunning1) {
        self.display1.text = [self elapsedTimeInterval:self.time1];
    }
    if (self.isRunning2) {
        self.display2.text = [self elapsedTimeInterval:self.time2];
    }
    if (self.isRunning1 || self.isRunning2) {
        [self performSelector:@selector(update) withObject:self afterDelay:0.1];
    }
}

- (NSString *)elapsedTimeInterval:(NSTimeInterval)timeInterval {
    NSTimeInterval currentTime = [NSDate timeIntervalSinceReferenceDate];
    if (timeInterval == 0) {
        timeInterval = currentTime;
    }
    int elapsedTime = currentTime - timeInterval;
    int minutes = elapsedTime / 60;
    int seconds = elapsedTime % 60;
    NSString *displayText = [NSString stringWithFormat:@"%u:%02u", minutes, seconds];

    return displayText;
}

- (IBAction)startPressed1:(UIButton *)button {
    self.running1 = !self.isRunning1;
    if (self.running1 == YES) {
        self.time1 = [NSDate timeIntervalSinceReferenceDate];
        [self update];
    }
    [self setTitleOfButton:button state:self.isRunning1];
}

- (IBAction)startPressed2:(UIButton *)button {
    self.running2 = !self.isRunning2;
    if (self.running2 == YES) {
        self.time2 = [NSDate timeIntervalSinceReferenceDate];
        [self update];
    }
    [self setTitleOfButton:button state:self.isRunning2];
}

- (void)setTitleOfButton:(UIButton *)button state:(BOOL)state {
    NSString *title = state ? @"Start" : @"Stop";
    [button setTitle:title forState:UIControlStateNormal];
}

@end

请注意,此代码中有许多不良做法,我故意没有解决这些问题。

答案 1 :(得分:0)

检查你的xib文件,更具体地说,检查display1和display2的连接是否正确。

复制/粘贴标签时,您可能错误地连接了标签。

相关问题