gpt4 book ai didi

ios - iOS NSTimer不调用选择器-不触发

转载 作者:行者123 更新时间:2023-12-01 17:31:44 25 4
gpt4 key购买 nike

我有:

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self setNeedsStatusBarAppearanceUpdate];
NSTimer* timer = [NSTimer scheduledTimerWithTimeInterval:0.25 target:self selector:@selector(setCurrentTime:) userInfo:nil repeats:YES];
[[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
[timer fire];

}
-(void)setCurrentTime{
NSLog(@"TEST");
dispatch_async(dispatch_get_main_queue(), ^{
NSDate *currentDate = [[NSDate alloc] init];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"HH:mm"];
[currentTime setText:[dateFormatter stringFromDate:currentDate]];
});
}

但是什么也没有得到。

最佳答案

您正在调用错误的选择器。您的“setCurrentTime”实现不带任何参数(例如,要正确地进行消息传递或调用,您应该使用“selector:@selector(setCurrentTime)”。

现在,如果您查看Apple's documentation for [NSTimer scheduledTimerWitTimeInterval: target: selector: userInfo: repeats:] ,Apple表示您的方法应具有以下签名:

- (void)setCurrentTime: (NSTimer *) timer

这意味着您的函数将如下所示:
-(void)setCurrentTime: (NSTimer *) timer
{
NSLog(@"TEST");
dispatch_async(dispatch_get_main_queue(), ^{
NSDate *currentDate = [[NSDate alloc] init];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"HH:mm"];
[currentTime setText:[dateFormatter stringFromDate:currentDate]];
});
}

并被这样称呼:
NSTimer* timer = [NSTimer scheduledTimerWithTimeInterval:0.25 
target:self
selector:@selector(setCurrentTime:)
userInfo:nil
repeats:YES];

关于ios - iOS NSTimer不调用选择器-不触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19217802/

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