PDA

View Full Version : the MOST n00b question of all n00b questions...




Darkroom
Jun 28, 2008, 01:20 PM
-(IBAction)methodA:(id)sender
{
if (something)
{
methodA code;
}
else
{
goto and execute (IBAction)methodB //how do i write this?
}
}

-(IBAction)methodB:(id)sender
{
metondB code;
}


how do i tell one method to direct itself to another method in the same class? also, is there a name for this type of thing, like a "redirect" or something like that?



yeroen
Jun 28, 2008, 01:35 PM
else
[self methodB: sender];

couldn't be simpler.

Darkroom
Jun 28, 2008, 01:38 PM
thanks, you're awesome! :D

MrFusion
Jun 28, 2008, 01:43 PM
First off, may I suggest you read a bit about the objective-c language/syntax? It's indeed quite a noob question. :)

This should work, I haven't tried compiling it, though.


@implementation MyClass

-(void) print:(NSString *)text {
NSLog(@"%@",text);
}
+(void) printnumber:(NSNumber *)number {
//class method, notice the +
NSLog(@"%i",[number intValue]);
}

-(NSNumber *) addnumber:(NSNumber *)n1 toNumber:(NSNumber *)n2 {
int temp = [n1 intValue]+[n2 intValue];
return [NSNumber numberWithInt:temp];
}

-(void) acutallyDoSomething {
[self print:@"hello world"];
NSNumber *a = [NSNumber numberWithInt:3];
NSNumber *b = [NSNumber numberWithInt:66];
NSNumber *c = [self addnumber:a toNumber:b];
[MyClass printnumber:a]; [MyClass printnumber:b]; [MyClass printnumber:c];
}

yoavcs
Jun 28, 2008, 01:48 PM
First off, may I suggest you read a bit about the objective-c language/syntax? It's indeed quit a noob question. :)

This should work, I haven't tried compiling it, though.



I fail to see what relation that chunk of code has to the OP's question.

MrFusion
Jun 28, 2008, 01:56 PM
I fail to see what relation that chunk of code has to the OP's question.

Just an example, perhaps a bit verbose maybe.

lee1210
Jun 28, 2008, 02:44 PM
I fail to see what relation that chunk of code has to the OP's question.

the example contained a message passed to self, which was what the OP was going for.

-Lee

Darkroom
Jul 7, 2008, 10:43 PM
else
[self methodB: sender];

couldn't be simpler.

ok, so my book "Absolute Beginner's Guide To C" hasn't arrived yet - expecting it any day now... but in the meantime:


[self methodB: sender];


works good if i'm trying to direct the method to an (IBAction)methodB:(id)sender. but what if i just want to redirect the method to a void method? if i want one method to direct itself to -(void)medhodC within the same class, than writing this:


[self methodC];


doesn't work...

i'm sure this is all very basic stuff to everyone, but while i'm waiting for my C book i would really appreciate a simple answer to this simple question...



[edit] ok, so perhaps writing [self methodC]; does in fact work and i'm way wrong... sorry... posted too soon again :o

lee1210
Jul 7, 2008, 11:06 PM
that should work fine if there is a methodC defined, and not medhodC. What warnings or errors do you get at compile time? At runtime?

A book on C is a great start, but is sadly wholly inapplicable to message passing which is Objective-C only.

-Lee
edit: My post missed your edit. Still not as fast on my phone as I am on a full keyboard.

Darkroom
Jul 8, 2008, 03:29 AM
that should work fine if there is a methodC defined, and not medhodC. What warnings or errors do you get at compile time? At runtime?.

i'm trying to go from a (void) method to an (IBAction) method, which doesn't seem to be as simple as i had hoped (based on the info from this thread)...


-(void)someFunction(NSTimer *)theTimer
{
//bla bla bla
[self actionMethod]; //[self actionMethod:sender] doesn't work either...
}

-(IBAction)actionMethod:(id)sender
{
//do something
}



[edit] ok, so i had to write [self actionMethod:theTimer], it seems to work now... but what confused me is that if it was a simple (void) function i was redirecting to (like, "-(void)something", i didn't have to write ":theTimer" in the line of code... don't understand why...

Mac Player
Jul 8, 2008, 04:50 AM
If you declare a function as having n arguments you must pass n arguments when you invoke it.

lee1210
Jul 8, 2008, 08:57 AM
Think of it this way... the declarations in your header file are a contract that states that the class and instances of the class will respond to particular messages. When you pass a message, the runtime will find the method associated with that message and invoke it. If you pass a message that does not match the prototypes in the header file, no method will be run. Normally you will get a compiler warning that the Object may not respond to that message. Due to inheritance there's a chance that you can ignore this if you know, for sure, the object will respond to the message you are passing. Otherwise, chances are the message pass will fail at runtime.

The return type of a method will never affect whether you can invoke it without assigning the result, so this won't stop you from making a call. The argument number and types do have an affect, so if you say something has no arguments but in your message you pass some, this doesn't match so the message fails. If you declare a method to take a single NSString * and you pass an int, the message pass will fail. You must pass things exactly as the method signature is defined.

Note that this is different than the behavior in C. There is no overloading in C. If there is a function called foo, foo will get called no matter how many arguments it takes or how many you pass. You may ruin the stack passing the wrong number of arguments, but nothing is going to stop you from doing so.

-Lee