视图控制器进入UITabBarController背景的问题

时间:2013-08-27 14:33:24

标签: ios objective-c uitabbarcontroller

我有一个UITabBarController,它连接了3个视图控制器。除了从一个视图切换到另一个视图时,一切都很好,新视图需要花一点时间来重新加载它正在做的事情。我知道这不是什么大问题,但它只是让应用看起来有点草率。有没有人知道如何让应用程序在后台运行,或者不需要每次都重新加载。

你可能已经注意到我对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中。在这两种方法之间,框架被重置,但我无法弄明白。

1 个答案:

答案 0 :(得分:1)

根据我的理解,当您在标签之间切换时,属性self.hourBarself.minuteBarself.secondBar的框架设置为0,并且每秒只更新一次。

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

作为旁注,你似乎来自C ++。 “ - >”对于Objective-C,表示法非常罕见,因为使用“。”访问属性,并使用“ - >”访问它们对应的实例变量。使用箭头符号将调用属性的自动合成getter / setter方法!

此外,是否有一个特定原因,您始终创建新的NSTimer对象而不是将repeats:设置为是?创建一个计时器并将其添加到runloop(scheduledTimerWith:...执行)是一个相对昂贵的操作。