-3

here is my code:

-(void)flash_random_winning_number:(NSInteger)param_which_magic_number
{


NSInteger dd = 9;


NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:.5
                          target:self
                          selector:@selector(ShowLabel22:dd:)
                           userInfo:Nil
                           repeats: YES
                  ];



}//flash_random_winning_number

the problem is with selector:@selector(ShowLabel22:dd:) because I am sending a parameter to this method called ShowLabel22:

-(void)ShowLabel:(NSInteger)param_which_magic_number
{
random_magic_number1.hidden = NO;
}

however if I were to remove the parameters from all this code, then there is no error. Therefore it seems as if I have mistake in the way I am using parameters.

  • 5
    Holy inconsistent naming conventions, Batman! Methods should always begin with a lower case letter. Method names and parameters should always be camelCased. That also isn't how you pass arguments to methods; simply tacking the variable onto the selector won't work. You should take a step back and focus on learning Objective-C. – bbum Dec 20 '13 at 15:45
  • First change name of your selector. And please read UIKit framework, to which object we can hide or show. – Bhumeshwer katre Dec 20 '13 at 15:48
  • thanks for the tip on methods bbum – user3120233 Dec 20 '13 at 16:03
  • There is no method named `ShowLabel22:dd:`. – Hot Licks Dec 20 '13 at 16:29

2 Answers2

2

The selector you pass to scheduledTimerWithTimeInterval:target:selector:userInfo:repeats: is akin to the name of a function. What you’re trying to do is to pass in the name of a function and the value of its parameters, but that won’t work. The method you name using @selector(...) will be passed exactly one argument: the NSTimer object calling the method.

If you have data you need to make available to ShowLabel it needs to be attached to the timer object or to self or something like that. To attach your number to the timer object you could do this:

- (void) flash_random_winning_number:(NSInteger) param_which_magic_number
{
    NSInteger dd = 9;
    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:.5
                                                      target:self
                                                    selector:@selector(ShowLabel:)
                                                    userInfo:@(dd)
                                                     repeats:YES];
}

- (void) ShowLabel:(NSTimer *)timer
{
    NSInteger dd = [[timer userInfo] integerValue];
    /* do stuff with dd */
}

(Since you’re new to Objective-C I want to mention the conventions that have arisen about method naming. The usual style, which you should always use, is for methods to be named with camelCasedNamesLikeThis. In particular, flash_random_winning_number and ShowLabel are both likely to confuse other Objective-C developers because they don’t “look like” method names. ShowLabel22 is also weird because of the numbers, but since you included that in one place in your question but not another I guess that’s just a typo.)

bdesham
  • 15,430
  • 13
  • 79
  • 123
0

Look at this SO answer. When you have a selector with arguments you have to use this method:

[self performSelector:@selector(myTest:) withObject:myString];
Community
  • 1
  • 1
Foriger
  • 459
  • 5
  • 30