溫馨提示×

IOS語法關于NStimer中scheduledTimerWithTimeInterval方法傳參的問題

iOS
小云
134
2023-09-21 04:02:38
欄目: 編程語言

在使用scheduledTimerWithTimeInterval方法創建NSTimer時,如果需要傳遞參數,可以使用userInfo參數來傳遞額外的數據。

下面是一個示例代碼:

- (void)startTimerWithInterval:(NSTimeInterval)interval {
NSDictionary *userInfo = @{@"param1": @"value1", @"param2": @"value2"};
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:interval
target:self
selector:@selector(timerFired:)
userInfo:userInfo
repeats:YES];
}
- (void)timerFired:(NSTimer *)timer {
NSDictionary *userInfo = timer.userInfo;
NSString *param1 = userInfo[@"param1"];
NSString *param2 = userInfo[@"param2"];
// 使用傳遞的參數進行相關操作
NSLog(@"param1: %@, param2: %@", param1, param2);
}

startTimerWithInterval方法中,通過userInfo參數將需要傳遞的參數存儲在一個NSDictionary對象中。然后,在timerFired:方法中,通過timer.userInfo獲取到傳遞的參數,并進行相關操作。

0
亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女