ios - View Controller 进入 UITabBarController 后台的问题
<p><p>我有一个连接了 3 个 ViewController 的 UITabBarController。除了从一个 View 切换到另一个 View 之外,一切都运行良好,新 View 需要一些时间来重新加载它正在执行的操作。我知道这没什么大不了的,但它只会让应用程序看起来有点草率。有谁知道我怎样才能让应用程序在后台运行,或者让它不必每次都重新加载。</p>
<p>您可能已经注意到,我对 Objective-C 很陌生,所以如果我不清楚,我可以理解,但非常感谢任何帮助!</p>
<p>编辑:给大卫</p>
<p>这是 .m 文件中秒表的代码:</p>
<pre><code>@implementation StopwatchViewController
- (BOOL)prefersStatusBarHidden
{
return YES;
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = ;
if (self) {
// Custom initialization
}
return self;
}
- (void)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;
;
} else {
self->isCounting = false;
;
}
}
- (void)startStopwatch
{
;
;
}
- (IBAction)resetStopwatch:(id)sender
{
;
}
- (void)stopwatch
{
if (self->isCounting == false) {
return;
} else {
NSInteger hourInt = ;
NSInteger minuteInt = ;
NSInteger secondInt = ;
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 *minuteString = ;
NSString *secondString = ;
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;
}
;
}
}
- (void)stopStopwatch
{
;
}
- (void)reset
{
;
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
{
;
// Dispose of any resources that can be recreated.
}
@end
</code></pre>
<p>大卫的第二次编辑:</p>
<p>将代码的主要部分更改为如下所示:</p>
<pre><code>- (void)viewWillDisappear:(BOOL)animated
{
;
;
}
- (void)viewWillAppear:(BOOL)animated
{
;
;
}
- (void)stopwatch
{
if (self->isCounting == false) {
return;
} else {
hourInt = ;
minuteInt = ;
secondInt = ;
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 *minuteString = ;
NSString *secondString = ;
hourLabel.text = hourString;
minuteLabel.text = minuteString;
secondLabel.text = secondString;
;
;
;
}
}
- (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;
}
</code></pre>
<p>我分离了代码,以便在 View 出现之前更新条形图。但是,它没有用。我通过在某些点打印出一些变量的值进行了一些调查。看来,在 viewWillAppear 中,secondBar.frame(和 minuteBar 等)已更新到正确的高度。但是,在 viewDidAppear 中,该值被重置为 0。这不会发生在 secondInt 或 secondFrame 上。在这两种方法之间的某个地方,框架被重置,但我无法弄清楚。</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>据我了解,您的属性 <code>self.hourBar</code>、<code>self.minuteBar</code> 和 <code>self.secondBar</code> 的框架在以下情况下设置为 0您在选项卡之间切换,并且每秒更新一次。</p>
<p>如果确实如此,只需在您的 viewControllers <code>viewWillAppear:</code> 方法中将它们设置为正确的值(将它们分配给 <code>viewWillDisappear:</code> 中的某个属性)。</p>
<p>作为旁注,您似乎来自 C++。 “->”符号在 Objective-C 中非常少见,因为属性是用“.”访问的,而它们对应的实例变量是用“->”访问的。使用箭头符号将<strong>不会</strong>调用属性的自动合成 getter/setter 方法!</p>
<p>另外,为什么您总是创建新的 <code>NSTimer</code> 对象而不是将 <code>repeats:</code> 设置为 yes,是否有特定的原因?创建一个计时器并将其添加到一个运行循环(<code>scheduledTimerWith:...</code> 所做的)是一项相对昂贵的操作。</p></p>
<p style="font-size: 20px;">关于ios -ViewController 进入 UITabBarController 后台的问题,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/18468369/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/18468369/
</a>
</p>
页:
[1]