• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - View Controller 进入 UITabBarController 后台的问题

[复制链接]
菜鸟教程小白 发表于 2022-12-12 13:47:41 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

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

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

编辑:给大卫

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

@implementation StopwatchViewController

- (BOOL)prefersStatusBarHidden
{
    return YES;
}

- (id)initWithNibNameNSString *)nibNameOrNil bundleNSBundle *)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)startOrStopid)sender
{
    if (self->isCounting == false) {
        self->isCounting = true;
        [self startStopwatch];
    } else {
        self->isCounting = false;
        [self stopStopwatch];
    }
}

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

- (IBAction)resetStopwatchid)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 selectorselector(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)viewWillDisappearBOOL)animated
{
    [super viewWillDisappear:YES];
    [self swapFrames];
}

- (void)viewWillAppearBOOL)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;
}

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



Best Answer-推荐答案


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

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

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

另外,为什么您总是创建新的 NSTimer 对象而不是将 repeats: 设置为 yes,是否有特定的原因?创建一个计时器并将其添加到一个运行循环(scheduledTimerWith:... 所做的)是一项相对昂贵的操作。

关于ios - View Controller 进入 UITabBarController 后台的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18468369/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap