在视图之间切换后更改标签

时间:2013-03-03 15:46:41

标签: iphone objective-c uiview timer

我有iPhone的双视图应用程序。在第一个我用NSTimer调用每秒执行此操作的代码:

  1. 更新变量
  2. 发出声音
  3. 更新标签
  4. 更新图像位置
  5. 当我启动应用程序时 - 一切都很完美。当我转到其他视图然后返回到第一个屏幕 - 1和2工作正常,但3和4重置并且根本不起作用。

    可能是什么问题?

    看起来它对元素失去了“钩子”......

    -(void)countup {
    DeccelFloat += 0.4;
    SpeedFloat += 5/(DeccelFloat);
    SpeedInt = lroundf(SpeedFloat);
    speed.text = [NSString stringWithFormat:@"%i", SpeedInt];
    imageview.center = CGPointMake(imageview.center.x, imageview.center.y+3);
    if (imageview.center.y < 280)  {
        imageview.alpha = 1;
        if (SpeedFloat > MaxSpeedValue) {
            [self playalarm];
        }else{
            [self playbeep]; 
        }
    }
    if ((imageview.center.y >= 280) && (imageview.center.y < 283))   {
        if (SpeedFloat > MaxSpeedValue) {
            [self playshot];
        }else{
            [self playbeepbeep];
        }
    }
    if ((imageview.center.y >= 280) && (imageview.center.y < 350))  {
        imageview.alpha = 0.5-(0.5/70)*(imageview.center.y - 280);
    }
    if (imageview.center.y >= 350)  {
        imageview.alpha = 0;
        imageview.center = CGPointMake(imageview.center.x, 180);
    }
    }
    

    这是我的计时器:

    - (void)viewDidLoad
    {
        [super viewDidLoad];
    
        MaxSpeedValue = 60;
    
    
        NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
    
        NSNumber *maxspeedsetttings = nil;
        NSNumber *blinkingsetttings = nil;
        NSNumber *startedsetttings = nil;
        if (standardUserDefaults) {
            maxspeedsetttings = [standardUserDefaults objectForKey:@"maxspeedsetttings"];
            blinkingsetttings = [standardUserDefaults objectForKey:@"blinkingsetttings"];
            startedsetttings = [standardUserDefaults objectForKey:@"startedsetttings"];
        }
    
        MaxSpeedDef = [maxspeedsetttings intValue];
        if (MaxSpeedDef == 1) {
            maxspeed.text = [NSString stringWithFormat:@"%i", MaxSpeedValue];
    
        }else{
            maxspeed.alpha = 0;
            maxspeedlabel.alpha = 0;
        }
    
    
    
    
            imageview.animationImages = [NSArray arrayWithObjects:
                                     [UIImage imageNamed:@"0.png"],
                                     [UIImage imageNamed:@"5.png"],
                                     [UIImage imageNamed:@"10.png"],
                                     nil];
            imageview.animationDuration = 1;
            imageview.animationRepeatCount = 0;
    
    
            BlinkingDef = [blinkingsetttings intValue];  
            if (BlinkingDef == 1) {
                [imageview startAnimating];
            }else{
                [imageview stopAnimating];
            }
            [self.view addSubview:imageview];
    
        StartedDef = [startedsetttings intValue];
        if (StartedDef == 0) {
            SpeedFloat = 0;
            DeccelFloat = 0;
            timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(countup) userInfo:nil repeats:YES];
    
            StartedDef = 1;
            if (standardUserDefaults) {
                [standardUserDefaults setObject:[NSNumber numberWithInt:StartedDef] forKey:@"startedsetttings"];
                [standardUserDefaults synchronize];
            }
        }
    
    }
    

0 个答案:

没有答案