如何觸發延遲,假設我想在3秒內調用一次方法(一次),並且如果需要,我如何取消該調用?延遲通話,可能取消?
在你的頭
5
A
回答
7
您還可以使用-[NSObject performSelector:awithObject:afterDelay:]
和+[NSObject cancelPreviousPerformRequestsWithTarget:selector:object]
。
1
..
NSTimer *timer;
當你想要安裝
..
timer = [NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(yourMethod:) userInfo:nil repeats:NO];
當你想取消
..
[timer invalidate];
3
使用的NSTimer。用三秒鐘的時間建立方法的呼叫。它只會被調用一次:
[NSTimer scheduledTimerWithTimeInterval: 3
target: self
selector: @selector(method:)
userInfo: nil
repeats: NO];
方法需要看起來像這樣:
- (void) method: (NSTimer*) theTimer;
可以傳遞參數到使用USERINFO方法(在上面設置爲零例)。它可以通過[theTimer userInfo]訪問。
在NSTimer上使用無效方法取消它。
+0
方法有沒有像這樣?那麼通過NSTimer實例需要做什麼? – 2008-12-03 15:35:28
+0
我認爲我說得對,該方法確實需要看起來像這樣。 userInfo參數用於傳入額外的數據。它的訪問方式是[theTimer userInfo]。 – 2008-12-03 16:05:14
相關問題
- 1. UISearchBar創建延遲/取消
- 2. iPhone取消延遲後排
- 3. 是否可以延遲或取消視圖消失?
- 4. 調用和通話之間的延遲
- 5. MDB消息消費延遲
- 6. 延遲獲取會話錯誤
- 7. 取消.Net 4.0中的任務延遲
- 8. 添加可延遲事件的延遲
- 9. 廚師客戶端取消通知延遲
- 10. rxjs5:延遲取消訂閱共享可觀察者
- 11. ALSA直通延遲
- 12. 延遲HTML5通知?
- 13. Android延遲通知
- 14. jQuery:超時/延遲與通知功能?
- 15. XMLHttpRequest延遲開始會話
- 16. 會話保存延遲php
- 17. 延遲對話框Java
- 18. PIC18F4550延遲功能
- 19. 春AMQP的消息延遲
- 20. ShowBalloonTip():消息出現延遲
- 21. NServiceBus延遲消息處理
- 22. RabbitMQ中的延遲消息
- 23. NServicebus延遲消息傳奇?
- 24. Websocket延遲發送消息
- 25. 延遲騾子服務可能嗎?
- 26. 這種延遲如何可能?
- 27. WCF性能,延遲和可伸縮性
- 28. 可能用`onEnter`延遲組件渲染?
- 29. 是否有可能延遲setAnimationDidStopSelector?
- 30. 延遲功能,直到$變爲可用
不要忘記在設置時保留計時器,並在使其無效後釋放計時器。依靠運行循環爲您保留它是一種糟糕的形式,如果Apple改變了實施方式,就有破壞風險的危險。 – 2008-12-03 17:49:12