Masaru Kitajima
Masaru Kitajima

Reputation: 267

An AppDelegate's property is unexpectedly set to null

First of all, I really appreciate your helps.

Well, I use three NSString objects in common in two views. And these view is segued by Embedded NavigationController, I mean I start programming with SingleView.

In AppDelegate.h, I write

@property (weak, nonatomic) NSString    *crntURL;

@property (weak, nonatomic) NSString    *crntTitle;

@property (weak, nonatomic) NSString    *crntHTML;

for delegation.

And in the first view, I have a webview and write

-(void)webViewDidFinishLoad:(UIWebView *)webView
{
    AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
    NSString    *url = [[NSString alloc] initWithString:[myWebView     stringByEvaluatingJavaScriptFromString:@"document.URL"]];
    NSString    *title = [[NSString alloc] initWithString:[myWebView stringByEvaluatingJavaScriptFromString:@"document.title"]];
    NSString    *html = [[NSString alloc] initWithString:[myWebView stringByEvaluatingJavaScriptFromString:@"document.all[0].outerHTML"]];
    appDelegate.crntTitle = nil;
    appDelegate.crntTitle = [[NSString alloc] initWithString:title];
    appDelegate.crntHTML = nil;
    appDelegate.crntHTML = [[NSString alloc] initWithString:html];
    appDelegate.crntURL = nil;
    appDelegate.crntURL = [[NSString alloc] initWithString:url];
}

Here, when I put NSLog, the expected HTML source code is dumped.

And in the second view(a subclass of UIViewController), I write

- (void)viewDidLoad
{
    // Do any additional setup after loading the view.
    AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
    sourceHTML.text = appDelegate.crntHTML;
    NSLog( @"%@", appDelegate.crntHTML );
    NSLog( @"%@", appDelegate.crntURL );
    NSLog( @"%@", appDelegate.crntTitle );
    [super viewDidLoad];
}

and only crntHTML is unexpectedly set to null while crntURL and crntTitle keep values.

Do you have any ideas?

Thank you in advance.

Masaru

Upvotes: 1

Views: 666

Answers (1)

Morten Fast
Morten Fast

Reputation: 6320

You've declared your properties in the app delegate as weak. Using ARC, an object will be released and set to nil if there's no strong reference to it.

I could imagine you're referencing the title and URL variable from the first view controller, but the HTML variable is only referenced in the second view controller. Once you're ready to show the HTML in the second controller, it has already been released, since the app delegate is not holding on to it.

Try changing the property declarations in the app delegate to strong:

@property (strong, nonatomic) NSString *crntURL;
@property (strong, nonatomic) NSString *crntTitle;
@property (strong, nonatomic) NSString *crntHTML;

Upvotes: 1

Related Questions