【问题标题】:updating UILabel from appDelegate从 appDelegate 更新 UILabel
【发布时间】:2013-05-15 03:08:01
【问题描述】:

我从过去几个小时就一直遇到这个问题,并尽我所能进行了所有搜索,但不幸的是,我没有找到任何可以解决我的问题的东西...... 场景:我在 TimerViewController 中有一个 CountDownTimer,在 AppDelegate 中设置了 NSTimer 和其他方法,假设更新 TimerViewController 的标签...根据标签的设置器,我正确地获取了值并显示在 NSLog 但是,标签没有在屏幕上更新...每秒从 AppDelegate 调用此设置器,并且标签应该显示计时器,

- (void)setMainTimerLabel:(UILabel *)mainTimerLabel 
  {
    _mainTimerLabel = mainTimerLabel;
    NSLog(@"ValueUpdated %@",_mainTimerLabel); 
  }

我已经仔细检查了标签,它与界面正确连接,我尝试使用测试字符串从 ViewDidLoad 更新标签,标签向我显示字符串... 请帮忙!

编辑: AppDelegate 代码:

AppDelegate.h

@property (nonatomic, strong) TimerViewController *TimerVC;

- (void)fireTimer;

AppDelegate.m

- (void)fireTimer
{
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(countDownTimer) userInfo:nil repeats:YES];
}

- (void) countDownTimer
{
    .......
   TimerVC = [[TimerViewController alloc]init];
    self.TimerVC.mainTimerLabel = [NSString stringWithFormat:@"%02d:%02d:%02d",hours,minutes,seconds];
    .......
}

【问题讨论】:

    标签: iphone ios objective-c nstimer


    【解决方案1】:

    我按照 jabobadilla 的以下代码解决了这个问题

    我实际上通过执行一个方法来解决它,该方法将在我的 AppDelegate 中检索 NSTimer 正在更新的值,因为当我离开视图并返回时,触发 NSTimer 的方法不再在主线程中.只要我的 NSTimer 有效,此方法就会循环。我还设置了延迟,允许 UI 更新值,然后再次执行该方法。这是代码,以防它帮助遇到类似问题的人。我是从chandan提供的建议中得到这个想法的,谢谢!!

    AppDelegate.h

    @interface AppDelegate : UIResponder <UIApplicationDelegate> {
    
    }
    
    @property (nonatomic, retain) NSTimer *countdownTimer;
    @property (nonatomic, retain) NSString *timeString;
    

    CountdownTimerViewController.h

    @interface CountdownTimerViewController : UIViewController {
    
    AppDelegate *appdelegate;
    
    }
    
    @property (strong, nonatomic) IBOutlet UILabel *labelCountdownTimer;
    
    @property (strong, nonatomic) IBOutlet UIButton *buttonStartTimer;
    @property (strong, nonatomic) IBOutlet UIButton *buttonStopTimer;
    
    - (IBAction)startTimer:(id)sender;
    - (IBAction)stopTimer:(id)sender;
    

    CountdownTimerViewController.m

    @implementation CountdownTimerViewController
    
    @synthesize labelCountdownTimer;
    
    - (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.
    
        //Instatiating Appdelegate
        if(!appdelegate)
            appdelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    
    }
    
    - (void) viewDidAppear:(BOOL)animated {
    
        if ([appdelegate.countdownTimer isValid]) {
            [self updateLabel];
        } else {
            labelCountdownTimer.text = @"00:00:00";
        }
    
    }
    
    - (void)didReceiveMemoryWarning
    {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    #pragma mark - Button Action Methods
    
    - (IBAction)startTimer:(id)sender {
    
        [self updateCounter];
    
    }
    
    - (IBAction)stopTimer:(id)sender {
    
        [appdelegate.countdownTimer invalidate];
        labelCountdownTimer.text = @"00:00:00";
    
    }
    
    int countLimit=30; //seconds
    NSDate *startDate;
    
    - (void)updateCounter {
    
        labelCountdownTimer.text = @"00:00:00";
        startDate = [NSDate date];
    
        appdelegate.countdownTimer = [NSTimer scheduledTimerWithTimeInterval:1.0/10.0
                                                                      target:self
                                                                    selector:@selector(countDown)
                                                                    userInfo:nil
                                                                     repeats:YES];
    
    }
    
      - (void)countDown {
    
        if([[NSDate date] timeIntervalSinceDate:startDate] >= countLimit) {
            [appdelegate.countdownTimer invalidate];
            return;
        }
        else {            
        NSDate *currentDate = [NSDate date];
        NSTimeInterval timeInterval = -([currentDate timeIntervalSinceDate:startDate]);
        NSDate *timerDate = [NSDate dateWithTimeIntervalSince1970:timeInterval];
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"mm:ss"];
        [dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0.0]];
        appdelegate.timeString = [dateFormatter stringFromDate:timerDate];
        labelCountdownTimer.text = appdelegate.timeString;
        }
    
    } 
    
    
    - (void) updateLabel {
    
        if ([appdelegate.countdownTimer isValid]) {
            labelCountdownTimer.text = appdelegate.timeString;
            [self performSelector:@selector(updateLabel) withObject:nil afterDelay:0.05];
        } 
    
    }
    

    【讨论】:

      【解决方案2】:

      您的 IBOutlet 可能有问题....

      尝试创建一个程序化的 UILabel 并将 _mainTimerLabel 值传递给该标签....

      这可能对你有帮助..

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-05-22
        • 2013-05-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-01-27
        • 1970-01-01
        相关资源
        最近更新 更多