Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3277274
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T19:21:01+00:00 2026-05-17T19:21:01+00:00

How can I pass a parameter to the method called by a NSTimer? My

  • 0

How can I pass a parameter to the method called by a NSTimer? My timer looks like this:

[NSTimer scheduledTimerWithTimeInterval:4 target:self selector:@selector(updateBusLocation) userInfo:nil repeats:YES];

and I want to be able to pass a string to the method updateBusLocation. Also, where am supposed to define the method updateBusLocation? In the same .m file that I create the timer?

EDIT:

Actually I am still having problems. I am getting the error message:

Terminating app due to uncaught exception ‘NSInvalidArgumentException’, reason: ‘* -[MapKitDisplayViewController updateBusLocation]: unrecognized selector sent to instance 0x4623600′

Here is my code:

- (IBAction) showBus {

//do something

[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateBusLocation) userInfo:txtFieldData repeats:YES];
[txtFieldData release];
 }


 - (void) updateBusLocation:(NSTimer*)theTimer
 {
      NSLog(@"timer method was called");
      NSString *txtFieldData = [[NSString alloc] initWithString:(NSString*)[theTimer userInfo]];
if(txtFieldData == busNum.text) {
    //do something else
    }
    }

EDIT #2:
Never mind your example code works fine thanks for the help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T19:21:01+00:00Added an answer on May 17, 2026 at 7:21 pm

    You need to define the method in the target. Since you set the target as ‘self’, then yes that same object needs to implement the method. But you could have set the target to anything else you wanted.

    userInfo is a pointer that you can set to any object (or collection) you like and that will be passed to the target selector when the timer fires.

    EDIT: … Simple Example:

    Set up the timer:

        NSTimer* timer = [NSTimer scheduledTimerWithTimeInterval:2.0 
                                  target:self 
                                  selector:@selector(handleTimer:) 
                                  userInfo:@"someString" repeats:NO];
    

    and implement the handler in the same class (assuming you’re setting the target to ‘self’):

    - (void)handleTimer:(NSTimer*)theTimer {
     
       NSLog (@"Got the string: %@", (NSString*)[theTimer userInfo]);
     
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

if i have a protected method, can i pass in a parameter where the
Simple task like make AJAX request , pass one parameter and return result, can
What's the best method to pass parameters to SQLCommand? You can do: cmd.Parameters.Add(@Name, SqlDbType.VarChar,
When I submit a form in HTML, I can pass a parameter multiple times,
Using logparser you can pass on parameters to the query you'd like to run
How to pass parameters to [WebMethod] in Asp.Net(C#) ? Can some one please explain
I'm working on a Perl script. How can I pass command line parameters to
You can pass a function pointer, function object (or boost lambda) to std::sort to
I was wondering how I can pass either an ArrayList, List <int > or
In a vxWorks Real-Time process, you can pass environment variables as one of the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.