2012-07-05 69 views
1

首先,我非常感谢您的帮助。AppDelegate的属性意外设置为空

那么,我在两个视图中使用了三个共同的NSString对象。这些视图被嵌入式NavigationController所困扰,我的意思是我开始用SingleView编程。

在AppDelegate.h,我写

@property (weak, nonatomic) NSString *crntURL; 

@property (weak, nonatomic) NSString *crntTitle; 

@property (weak, nonatomic) NSString *crntHTML; 

进行委派。

而在第一种观点,我有一个网页视图,写

-(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保留值。

你有什么想法吗?

预先感谢您。

Masaru

+0

为什么所有以'[的NSString initWithString]'电话?前几个可能是好的,但最后三个是完全多余的。 – Mac

+0

前几个也完全是多余的。局部变量隐含地是'__strong',所以'alloc' /'initWithString'业务不需要做引用计数帮助。如果你的意思是确保你的一个变量持有一个单独的字符串副本,最好使用'copy'。 – rickster

回答

1

您已声明您在应用程序委托中的属性较弱。 使用ARC时,如果没有强引用,则会释放对象并将其设置为零。

我可以想象你正在引用来自第一个视图控制器的标题和URL变量,但是HTML变量仅在第二个视图控制器中被引用。一旦准备好在第二个控制器中显示HTML,它已经被释放,因为应用程序委托并没有保留它。

尝试在应用程序的委托更改属性声明强:

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

我很抱歉迟到。 谢谢你的解释。 –