Problems with NSTimer

Discussion in 'Mac Programming' started by krmarien, Feb 9, 2010.

  1. krmarien macrumors newbie

    Joined:
    Jan 8, 2010
    #1
    Hello,

    I have created an instance of NSTimer:
    Code:
    	checkTimer = [[NSTimer scheduledTimerWithTimeInterval: (time * 0.1f) target:self selector:@selector(checkLoggedIn) userInfo:self repeats:true] retain];
    
    But in the checkLoggedIn function I want to access functions of the where I create the timer. But how do I send these as parameters to the function checkLoggedIn.

    Code:
    	checkTimer = [[NSTimer scheduledTimerWithTimeInterval: (time * 0.1f) target:self selector:@selector(checkLoggedIn:) userInfo:self repeats:true withObject:@selector(fn1):@selector(fn2)] retain];
    
    This doesn't work.

    Thanks,
    Kristof
     
  2. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    I assume "I want to access functions of the where I create the timer" means "I want to be able to call methods on the object that created the timer". If it does then passing self as the userInfo is correct. You can simply call against the userInfo method in checkLoggedIn:. Note the :. It is very important: if you are passing a userInfo object you must code your method to expect this (typed as id). I'm pretty sure that's covered in the documentation.

    Your second example is entirely nonsensical.
     
  3. krmarien thread starter macrumors newbie

    Joined:
    Jan 8, 2010
    #3
    Ok, but how do I get the selector of a funtion inside checkLoggedIn?

    Code:
    - (void)checkLoggedIn:(id)sender
    {
    	check = [CheckLoggedIn alloc];
    	[check checkLoggedIn:sender loggedInSelector:@selector([sender loggedIn]) notloggedInSelector:@selector([sender notLoggedIn])];
    }
    This doesn't work
     
  4. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #4
    Selectors don't know about objects. Think of them as strings. You just create and pass them around wherever you want, without knowledge of an object or class.

    You may want to read up on them, as your syntax is slightly off:
    http://developer.apple.com/mac/libr...nceptual/ObjectiveC/Articles/ocSelectors.html

    If you want to send selectors as arguments to a timer, the easiest way is to use NSStringFromSelector and if you have more than one, use a dictionary.
     

Share This Page