如何定期调用 Objective C 中的方法?

发布于 2024-12-22 14:21:46 字数 350 浏览 1 评论 0原文

如果问题没有解释清楚请原谅。 我正在开发一个 iPhone 客户端-服务器应用程序,我创建了所有的类、实例等。我什至可以发送 get 并解析响应。无论如何,现在我需要让我的方法在定义的时间段内被调用(例如,在 10 秒内重复调用它)。我用谷歌搜索了很多,也看看

NSDate 但我无法解决.. 现在,有人可以帮我如何处理这种情况吗?非常感谢

if the question is not explained clearly please excuse me.
I'm developing an iphone Client-Server app, i created all the classes, instances and ect. I can even send get and parse the response too.. Anyway, now i need to make my method be called in a defined period of time(for instance, call it repeatly in 10 seconds). I googled a lot and also take a look at

NSDate but i couldn't solve.. Now, can anyone please help me how to handle this situation? Thank you very much

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

筑梦 2024-12-29 14:21:46

您可以创建并安排一个 NSTimer 实例,以在给定的时间间隔内调用您的方法。特别请参阅以下便捷创建方法:

+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)seconds target:(id)target selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)repeats

http: //developer.apple.com/library/ios/#documentation/cocoa/reference/foundation/Classes/NSTimer_Class/Reference/NSTimer.html

You can create and schedule an instance of NSTimer to call your method on a given time interval. See in particular the following convenience creation method:

+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)seconds target:(id)target selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)repeats

http://developer.apple.com/library/ios/#documentation/cocoa/reference/foundation/Classes/NSTimer_Class/Reference/NSTimer.html

反差帅 2024-12-29 14:21:46

您最好的选择是查看 Grand Central Dispatch 因为您想要在后台运行它:

使用上面提到的 NSTimer

Your best bet is to look into Grand Central Dispatch since you are going to want to run this in the background:

Use NSTimer mentioned above.

天气好吗我好吗 2024-12-29 14:21:46

您想要的是 NSObject 方法 -performSelector:withObject:afterDelay:

点击查看文档

如果您有它调用的方法再次调用它本身,您将拥有一个循环的、自触发的延迟轮询方法。

我建议检查一个类变量,看看您每次是否真的这么想,这样您就可以从外部将其关闭。

What you want is the NSObject method -performSelector:withObject:afterDelay:.

Click for docs.

If you have the method that it calls call this on itself again, you'll have a looping, self-firing delayed poll method.

I'd recommend checking a class variable to see if you really mean it each time, so you can turn it off from outside itself.

千紇 2024-12-29 14:21:46

Grand Central Dispatch 将创建一个不同的线程来运行。因此,如果计时器方法(如下所示和上面建议的)滞后于您的应用程序,您将需要将该命令放在单独的线程上。

NSTimer 是你应该使用的。例如,如果您想重复从按下按钮启动的方法,您可以执行此操作

- (void)viewDidLoad
{
    [super viewDidLoad];
    [cameraControlButtonUp addTarget:self action:@selector(cameraControlButtonUpPressed) 
          forControlEvents:UIControlEventTouchUpInside];
}

-(IBAction)buttonDown:(id)sender{
     NSInteger tag = [sender tag];

    if (tag==1) {  
        buttonCounter=1;
        timer = [[NSTimer scheduledTimerWithTimeInterval:.5 target:self selector:@selector(sendJoin) userInfo:nil repeats:YES]retain];
    }
}

-(void)sendJoin
{
    switch (buttonCounter) {
        case 1:
            [cClient userDigitalPushAndRelease:372];
            break; 
        default:
            break;
    }
}

    -(void)cameraControlButtonUpPressed
    {
        [timer invalidate];
    }

,重复该命令直到释放按钮。请记住,您需要将 ibaction 与按钮按下事件链接起来(仅按钮按下事件)。以及在 .h 中创建计时器并将要使用它的按钮标记为 1。

一个更基本的例子;这很简单。只需创建调用方法、计时器并将重复设置为“是”即可。然后调用 invalidate 来停止它。我必须创建一个单独的方法 sendJoin 因为我无法将数字正确传递给该方法。但如果你没有任何参数,那就更容易了。只需使用计时器语法来创建它,然后在完成后使其无效

Grand Central Dispatch will create a different thread to run on. so if the timer method (shown below and suggested above) lags your app you will need to put the command on a separate thread.

NSTimer is what you should use though. for example if you want to repeat a method that is initiated from a button press you could do this

- (void)viewDidLoad
{
    [super viewDidLoad];
    [cameraControlButtonUp addTarget:self action:@selector(cameraControlButtonUpPressed) 
          forControlEvents:UIControlEventTouchUpInside];
}

-(IBAction)buttonDown:(id)sender{
     NSInteger tag = [sender tag];

    if (tag==1) {  
        buttonCounter=1;
        timer = [[NSTimer scheduledTimerWithTimeInterval:.5 target:self selector:@selector(sendJoin) userInfo:nil repeats:YES]retain];
    }
}

-(void)sendJoin
{
    switch (buttonCounter) {
        case 1:
            [cClient userDigitalPushAndRelease:372];
            break; 
        default:
            break;
    }
}

    -(void)cameraControlButtonUpPressed
    {
        [timer invalidate];
    }

that will repeat the command till the button is released. take in mind you need to link the ibaction with the button down event (only the button down event). as well as create timer in the .h and tag the button to 1 that you want to use this with.

for a more basic example; its quite simple. just create your method to call, timer and set repeat to YES. then call invalidate to stop it. i had to create a seperate method sendJoin because i couldnt get the numbers to pass correctly to the method. but if you dont have any parameters its even easier. just use the timer syntax to create it then invalidate it when ur done

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文