views:

42

answers:

2

Hi,

I have two classes, Class A and Class B, both of them are subclasses of UIViewController.

I class A I have an NSString and I want to use this NSString in class B.

ClassA.h:

@class ClassB;
@interface ClassA : UIViewController {
   ClassB *classB;
   NSString stringA;
}
@property (nonatomic, retain) ClassB *classB;
@property (nonatomic, retain) NSString *stringA;

@end

ClassA.m:

stringA = [NSString stringWithString:webView.request.URL.absoluteString];

ClassB.h:

@class ClassA;
@interface ClassA : UIViewController {
   ClassB *classA;
   NSString stringB;
}
@property (nonatomic, retain) ClassB *classA;
@property (nonatomic, retain) NSString *stringB;

@end

ClassB.m:

- (void)viewWillAppear:(BOOL)animated { 
self.stringB = classA.stringA;
}

Of course I did #import for both classes. For some reason I always get NULL I classB for stringB.

Thanks!

+1  A: 

The following aren't clear:

  • is mainViewController actually an instance of ClassA?
  • is classA even an instance of ClassA, as you've declared it an instance of ClassB?
  • what is your real code, as the things you've pasted here don't compile?
  • when in the ClassA object's lifecycle do you initialise stringA?
  • did that occur before you tried to use it in your ClassB object?
Graham Lee
1. I'm sorry, I edited the code because I have changed it to ClassA and ClassB.2. No.4. I didn't initialise stringA
Guy Dor
If you haven't initialised stringA, why is it surprising that its value is nil when assigned to stringB?
Graham Lee
Actually I did initialised the string,Here's how the string gets it's code:- (void)shareLeftAction:(id)sender { self.stringA = webView.request.URL.absoluteString; [self presentModalViewController:sharingController animated:YES];}Then an UIViewController is presented with the string value.
Guy Dor
A: 

Hey, I would like to comment one thing you have high probability of RetainLoop, while ClassA retains ClassB and ClassB retains ClassA. When do they release?

Second thing, in:

ClassA.m:

stringA = [NSString stringWithString:webView.request.URL.absoluteString];

change to:

self.stringA = [NSString stringWithString:webView.request.URL.absoluteString];

while object returned by [NSString stringWithString:] is set to autorelease, and you need to retain it to be sure that you have valid instance of string.

Please provide more code.

lukewar
Thank you for helping, thats the most relevant code that I can provide
Guy Dor