How to pass arguments when calling function with timer in objective c

后端 未结 7 1524
我寻月下人不归
我寻月下人不归 2020-12-20 00:02
-(void)setX:(int)x andY:(int)y andObject:(Sprite*)obj
{
    [obj setPosition:CGPointMake(x,y)];
}

Now, I want to call above method, using following

7条回答
  •  半阙折子戏
    2020-12-20 00:27

    Copied from an answer by Matt Ball:

        - (void)startMyTimer {
            /* ... Some stuff ... */
            NSDictionary *userDict;
            userDict = [NSDictionary dictionaryWithObjectsAndKeys:someValue,
                                                                  @"value1",
                                                                  someOtherValue,
                                                                  @"value2", nil];
    
            [NSTimer scheduledTimerWithTimeInterval:0.1
                                             target:self
                                           selector:@selector(callMyMethod:)
                                           userInfo:userDict
                                            repeats:YES];
    }
        - (void)callMyMethod:(NSTimer *)theTimer {
            NSString *value1 = [[theTimer userInfo] objectForKey:@"value1"];
            NSString *value2 = [[theTimer userInfo] objectForKey:@"value2"];
            [self myMethod:value1 setValue2:value2];
        }
    

提交回复
热议问题