Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Darkroom

Guest
Original poster
Dec 15, 2006
2,445
0
Montréal, Canada
Code:
-(IBAction)methodA:(id)sender
	{
	if (something)
		{
		methodA code;
		}
		else
		{
		[COLOR="Purple"][B]goto and execute (IBAction)methodB[/B][/COLOR]  [COLOR="SeaGreen"]//how do i write this?[/COLOR]
		}
	}
	
-(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?
 

MrFusion

macrumors 6502a
Jun 8, 2005
613
0
West-Europe
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.

Code:
@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

macrumors regular
Apr 7, 2004
220
96
Israel
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.
 

Darkroom

Guest
Original poster
Dec 15, 2006
2,445
0
Montréal, Canada
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:

Code:
[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:

Code:
[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 :eek:
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
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

Guest
Original poster
Dec 15, 2006
2,445
0
Montréal, Canada
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)...

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

-(IBAction)actionMethod:(id)sender
{
[COLOR="SeaGreen"]//do something[/COLOR]
}


[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

macrumors regular
Jan 19, 2006
225
0
If you declare a function as having n arguments you must pass n arguments when you invoke it.
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
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
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.