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

kurushetra

macrumors newbie
Original poster
Mar 19, 2009
11
0
hello i am new in objective c and i have a question ¿why i get a (EXC_BAD_ACCESS) when i call the method "primera"?
the code is this:
#import <Cocoa/Cocoa.h>

@interface cont : NSObject {
IBOutlet id text;
}
- (IBAction)boton:(id)sender;
-(void)primera;

@end
and:
#import "cont.h"

@implementation cont
NSString *mensaje;
- (IBAction)boton:(id)sender {
mensaje = (@"hola");
cont *con;
[con primera];
}
-(void)primera{
[text setStringValue:(mensaje)];
}

@end
i know i can run the action from (- (IBAction)boton:(id)sender) but this is only one example. why is not possible to call another method from (-(IBAction)boton:(id)sender) also if the method "primera" is with (+) give me a warning (instance variable "text" accesed in class method ) ¿can somebody tell me how i can call the method "primera" from ((IBAction)boton:(id)sender)?
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
What is con supposed to be? In your code it's a pointer to a cont, but it's never initialised so it's just pointing at no-where. It's certainly not pointing at the instance of the class that the method is within if that's what you were hoping.

Go back to basic C. If I declare a pointer and then never set where it points to what happens when I deference that pointer? The program crashes. Same here. In all seriousness you need to ensure you know your C as this is a basic-level failure.

I suggest you read Apple's Introduction to Objective-C. You should have read and understood all of this before trying to write any code. It will tell you how to call methods on the same object as the current method is operating on, specifically in this section.
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
hello i am new in objective c and i have a question ¿why i get a (EXC_BAD_ACCESS) when i call the method "primera"?
the code is this:
Code:
#import <Cocoa/Cocoa.h>

@interface cont : NSObject {
    IBOutlet id text;
}
- (IBAction)boton:(id)sender;
-(void)primera;
 
@end
and:
#import "cont.h"

@implementation cont
NSString *mensaje;
- (IBAction)boton:(id)sender {
    mensaje = (@"hola");
	cont *con;
	[con primera];
}
-(void)primera{
	[text setStringValue:(mensaje)];
}
 
@end
i know i can run the action from (- (IBAction)boton:(id)sender) but this is only one example. why is not possible to call another method from (-(IBAction)boton:(id)sender) also if the method "primera" is with (+) give me a warning (instance variable "text" accesed in class method ) ¿can somebody tell me how i can call the method "primera" from ((IBAction)boton:(id)sender)?

Just say:
Code:
[self primera];

Right now you are declaring a new cont *, not initializing it, then trying to send it the message primera. self is the instance of cont you actually want to send that message to.

-Lee

Edit: I won't erase this since i've already posted it, but robbieduncan's point is good... this is not particularly advanced, and you should understand why things went so wrong, not just use the code above and continue without learning anything.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.