views:

89

answers:

2

In ClassA:

- (ClassA *)initWithID:(NSString *) cID andTitle:(NSString *) cTitle {
    ClassAID = cID;
    ClassATitle = cTitle;
    return self;
}

In ClassB:

- (void)cellDidSelected {
    ClassA *classAController = [[ClassA alloc] init];
//Program received signal:  “EXC_BAD_ACCESS” when executing the following line.
    classAController = [classAController initWithClassAID:ClassAID andClassATitle:ClassATitle];
    NSLog(@"I want to get the value of ID:%@ and Title:%@ here.", [classAController ClassATitle], [classAController ClassAID])
}

Could anyone point where is wrong? Thanks a lot.

A: 

Typically the structure one would design for the situation you've posted above would include the call to [super init] within initWithID itself, so you would only have one init routine called per object instantiation. However I don't see that being the root cause of the problem you're seeing.

fbrereto
Seeing your post, I try it again, and still failed. I've initialed the classAController and pass it as the parameter.Why do I still need to invoke [super init]? Thanks.
Because the super object needs to be initialised as well as your object.
JeremyP
Thanks both answer!
+1  A: 

Try using:

- (id)initWithID:(NSString*) cID andTitle:(NSString*) cTitle {

    if (!(self = [super init]))
       return nil;

    ClassAID = cID;
    ClassATitle = cTitle;
    return self;
}

Then you can just do something like:

ClassA * classA = [[ClassA alloc] initWithID:anID andTitle:aTitle];

And I would recommend having ClassAID and ClassATitle as properties if they're not already, and if they are you should be using:

[self setClassAID:cID];
[self setClassATitle:cTitle];

That way they'll be retained properly.

Tom Irving
This solution should also mention the removal of the [[ClassA alloc] init] line, but is the best of those available thus far.
fbrereto
it still didn't work for me.../_\, thanks though.
It what way did it not work?
Tom Irving