2

我有一个带有 3 个视图控制器的 UITabBarController 连接到它。除了从一个视图切换到另一个视图之外,一切都运行良好,新视图需要一些时间来重新加载它正在执行的操作。我知道这没什么大不了的,但它只会让应用程序看起来有点草率。有谁知道我怎样才能让应用程序在后台运行,或者让它不必每次都重新加载。

正如您可能已经注意到的那样,我对 Objective-C 非常陌生,所以如果我不清楚,我可以理解,但非常感谢任何帮助!

编辑:大卫

这是 .m 文件中秒表的代码:

@implementation StopwatchViewController

- (BOOL)prefersStatusBarHidden
{
    return YES;
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.

    isCounting = false;
}

- (IBAction)startOrStop:(id)sender
{
    if (self->isCounting == false) {
        self->isCounting = true;
        [self startStopwatch];
    } else {
        self->isCounting = false;
        [self stopStopwatch];
    }
}

- (void)startStopwatch
{
    [startStopButton setTitle:@"STOP" forState:UIControlStateNormal];
    [self performSelector:@selector(stopwatch) withObject:self afterDelay:1.0];
}

- (IBAction)resetStopwatch:(id)sender
{
    [self reset];
}

- (void)stopwatch
{
    if (self->isCounting == false) {
        return;
    } else {

    NSInteger hourInt = [hourLabel.text intValue];
    NSInteger minuteInt = [minuteLabel.text intValue];
    NSInteger secondInt = [secondLabel.text intValue];

    if (secondInt == 59) {
        secondInt = 0;
        if (minuteInt == 59) {
            minuteInt = 0;
            if (hourInt == 23) {
                hourInt = 0;
            } else {
                hourInt += 1;
            }
        } else {
            minuteInt += 1;
        }
    } else {
        secondInt += 1;
    }

    NSString *hourString = [NSString stringWithFormat:@"%02d", hourInt];
    NSString *minuteString = [NSString stringWithFormat:@"%02d", minuteInt];
    NSString *secondString = [NSString stringWithFormat:@"%02d", secondInt];

    hourLabel.text = hourString;
    minuteLabel.text = minuteString;
    secondLabel.text = secondString;

    CGRect hourFrame = self->hourBar.frame;
    CGRect minuteFrame = self->minuteBar.frame;
    CGRect secondFrame = self->secondBar.frame;

    if ((NSInteger)hourFrame.size.height != hourInt) { // check if we need to modify
        hourFrame.origin.y -= ((hourInt * 10.0) - hourFrame.size.height);
        hourFrame.size.height = (hourInt * 10.0);

        self->hourBar.frame = hourFrame;
    }

    if ((NSInteger)minuteFrame.size.height != minuteInt) { // check if we need to modify
        minuteFrame.origin.y -= ((minuteInt * 4.0) - minuteFrame.size.height);
        minuteFrame.size.height = (minuteInt * 4.0);

        self->minuteBar.frame = minuteFrame;
    }

    if ((NSInteger)secondFrame.size.height != secondInt) { // check if we need to modify
        secondFrame.origin.y -= ((secondInt * 4.0) - secondFrame.size.height);
        secondFrame.size.height = (secondInt * 4.0);

        self->secondBar.frame = secondFrame;
    }

    [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(stopwatch) userInfo:nil repeats:NO];
    }
}

- (void)stopStopwatch
{
    [startStopButton setTitle:@"START" forState:UIControlStateNormal];
}

- (void)reset
{
    [startStopButton setTitle:@"START" forState:UIControlStateNormal];

    self->isCounting = false;

    hourLabel.text = @"00";
    minuteLabel.text = @"00";
    secondLabel.text = @"00";

    CGRect hourFrame = self->hourBar.frame;
    CGRect minuteFrame = self->minuteBar.frame;
    CGRect secondFrame = self->secondBar.frame;

    hourFrame.size.height = 0;
    minuteFrame.size.height = 0;
    secondFrame.size.height = 0;

    hourFrame.origin.y = 321.0;
    minuteFrame.origin.y = 321.0;
    secondFrame.origin.y = 321.0;

    self->hourBar.frame = hourFrame;
    self->minuteBar.frame = minuteFrame;
    self->secondBar.frame = secondFrame;
}

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

@end    

大卫的第二次编辑:

将代码的主要部分更改为如下所示:

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:YES];
    [self swapFrames];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    [self updateBars];
}

- (void)stopwatch
{
    if (self->isCounting == false) {
        return;
    } else {

    hourInt = [hourLabel.text intValue];
    minuteInt = [minuteLabel.text intValue];
    secondInt = [secondLabel.text intValue];

    if (secondInt == 59) {
        secondInt = 0;
        if (minuteInt == 59) {
            minuteInt = 0;
            if (hourInt == 23) {
                hourInt = 0;
            } else {
                hourInt += 1;
            }
        } else {
            minuteInt += 1;
        }
    } else {
        secondInt += 1;
    }

    NSString *hourString = [NSString stringWithFormat:@"%02d", hourInt];
    NSString *minuteString = [NSString stringWithFormat:@"%02d", minuteInt];
    NSString *secondString = [NSString stringWithFormat:@"%02d", secondInt];

    hourLabel.text = hourString;
    minuteLabel.text = minuteString;
    secondLabel.text = secondString;

    [self swapFrames];
    [self updateBars];

    [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(stopwatch) userInfo:nil repeats:NO];
    }
}

- (void)updateBars
{
    if ((NSInteger)hourFrame.size.height != hourInt) { // check if we need to modify
        hourFrame.origin.y -= ((hourInt * 10.0) - hourFrame.size.height);
        hourFrame.size.height = (hourInt * 10.0);

        self->hourBar.frame = hourFrame;
    }  

    if ((NSInteger)minuteFrame.size.height != minuteInt) { // check if we need to modify
        minuteFrame.origin.y -= ((minuteInt * 4.0) - minuteFrame.size.height);
        minuteFrame.size.height = (minuteInt * 4.0);

        self->minuteBar.frame = minuteFrame;
    }

    if ((NSInteger)secondFrame.size.height != (secondInt * 4.0)) { // check if we need to modify
        secondFrame.origin.y -= ((secondInt * 4.0) - secondFrame.size.height);
        secondFrame.size.height = (secondInt * 4.0);

        self->secondBar.frame = secondFrame;
    }
}

- (void)swapFrames
{
    hourFrame = self->hourBar.frame;
    minuteFrame = self->minuteBar.frame;
    secondFrame = self->secondBar.frame;
}

我分离了代码,以便在视图出现之前它应该更新条形图。但是,它没有用。我通过在某些点打印出一些变量的值进行了一些调查。看来,在 viewWillAppear 中,secondBar.frame(和 minuteBar 等)已更新到正确的高度。但是,在 viewDidAppear 中,该值被重置为 0。这不会发生在 secondInt 或 secondFrame 上。在这两种方法之间的某个地方,框架被重置,但我无法弄清楚。

4

1 回答 1

1

据我了解,当您在选项卡之间切换时,您的属性的框架self.hourBarself.minuteBar设置self.secondBar为 0,并且仅每秒更新一次。

如果确实如此,只需在 viewControllersviewWillAppear:方法中将它们设置为正确的值(将它们分配给 中的某个属性viewWillDisappear:)。

作为旁注,您似乎来自 C++。“->”符号在 Objective-C 中非常少见,因为属性是用“.”访问的,而它们对应的实例变量是用“->”访问的。使用箭头符号不会调用属性的自动合成 getter/setter 方法!

NSTimer此外,您总是创建新对象而不是设置为“是”是否有特定原因repeats:?创建计时器并将其添加到运行循环(scheduledTimerWith:...确实如此)是一项相对昂贵的操作。

于 2013-08-27T14:50:32.957 回答