简体   繁体   English

有没有办法在程序在 Objective-C 中运行时不断调用方法?

[英]Is there a way to be calling a method constantly while the program is running in Objective-C?

I want to call a method infinitely while the program is running to check for the change of state of something.我想在程序运行时无限调用一个方法来检查 state 的变化。 Is it possible to do this when programming for the iPhone?为 iPhone 编程时可以这样做吗?

It is possible, just use a repeating NSTimer .有可能,只需使用重复的NSTimer

However I would advise against the continuous polling to get the state - better to setup a callback/delegate/notification pattern to notify any observers of a state change.但是,我建议不要连续轮询以获取 state - 最好设置回调/委托/通知模式以通知任何观察者 state 更改。

You could also use Key Value Observing (KVO) to get notified when a value is updated.您还可以使用键值观察 (KVO) 在值更新时获得通知。

Lots of food for thought and terms to research.大量的思考和研究术语。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM