Reputation:
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!
Upvotes: 0
Views: 243
Reputation: 2715
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.
Upvotes: 0
Reputation:
The following aren't clear:
mainViewController
actually an instance of ClassA
?classA
even an instance of ClassA
, as you've declared it an instance of ClassB
?ClassA
object's lifecycle do you initialise stringA
?ClassB
object?Upvotes: 1