gpt4 book ai didi

iphone - 从 appDelegate 更新 UILabel

转载 作者:行者123 更新时间:2023-11-29 13:13:27 25 4
gpt4 key购买 nike

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

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

我仔细检查了标签,它正确地连接了接口(interface),我尝试用测试字符串从 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];
.......
}

最佳答案

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

我实际上通过执行一个方法来解决它,该方法将去检索 NSTimer 在我的 AppDelegate 中更新的值,因为当我离开 View 并返回它时,触发 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];
}

}

关于iphone - 从 appDelegate 更新 UILabel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16556129/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com