AppDelegate的属性意外设置为null

时间:2012-07-05 04:34:51

标签: objective-c delegates nsstring

首先,我非常感谢你的帮助。

好吧,我在两个视图中共同使用了三个NSString对象。这些视图是由Embedded NavigationController引发的,我的意思是我开始使用SingleView进行编程。

在AppDelegate.h中,我写了

@property (weak, nonatomic) NSString    *crntURL;

@property (weak, nonatomic) NSString    *crntTitle;

@property (weak, nonatomic) NSString    *crntHTML;

代表团。

在第一个视图中,我有一个webview并写

-(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];
}

这里,当我放置NSLog时,会转储预期的HTML源代码。

在第二个视图(UIViewController的子类)中,我写了

- (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];
}

并且只有crntHTML意外地设置为null,而crntURL和crntTitle保持值。

你有什么想法吗?

提前谢谢。

1 个答案:

答案 0 :(得分:1)

您已将应用委托中的属性声明为弱。 使用ARC时,如果对象没有强引用,则会释放一个对象并设置为nil。

我可以想象你从第一个视图控制器引用了title和URL变量,但HTML变量只在第二个视图控制器中引用。一旦你准备好在第二个控制器中显示HTML,它就已经被释放了,因为应用代表没有坚持它。

尝试将应用委托中的属性声明更改为strong:

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