How to call a method every x seconds in Objective-C using NSTimer?

前端 未结 4 1812
鱼传尺愫
鱼传尺愫 2020-12-24 14:12

I am using Objective-C, Xcode 4.5.1 and working on an app for the iPhone.

I have a method A in which I want to call another method B to do a series of calculations e

4条回答
  •  我在风中等你
    2020-12-24 14:50

    Well you are trying to call an normal C method, NSTimer can't to that.

    The target is the an instance of the class on which to call the selector, this selector adn not select. The selector here is a SEL type which you can create with the @selector(METHOD_NAME) function.

    For example this will call the handleTimer : ever 0.1 second: (For this example the AppDelegate is used):

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
        //SNIP, some code to setup the windos.   
    
        [NSTimer scheduledTimerWithTimeInterval:0.1f target:self selector:@selector(handleTimer:) userInfo:nil repeats:YES];
        return YES;
    }
    
    - (void) handleTimer:(NSTimer *)timer {
        // Hanlde the timed event.
    }
    

提交回复
热议问题