0

edit 2

I found out it depends on the method used. I somehow lose the appdelegate pointer when I access it via scrollViewDidEndDecelerating instead of awakeFromNib. But I do need to use scrollViewDidEndDecelerating. Can someone of you please fix this?

Works fine

- (void)awakeFromNib
{
AppDelegate *mainDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
NSLog(@"%@", [NSString stringWithFormat:@"%@", mainDelegate.content1]);
}

Crashes bad_access to mainDelegate.content1

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
AppDelegate *mainDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
NSLog(@"%@", [NSString stringWithFormat:@"%@", mainDelegate.content1]);
}

ORIGINAL QUESTION (I keep this for past-reference. Just read the above functions)

This problem regards stringWithContentsOfURL

The code below works, downloads HTMLstring to mainDelegate.content1 and shows it appropriately in UIWebView controller.myWebView. If I modify the function, so that I conduct the downloading process beforehand, let it be somewhere in AppDelegate.m, the program crashes upon processing mainDelegate.content1 in this very function.

Here goes the good code

- (void)ReloadView:(int)page{

MyViewController *controller = [viewControllers objectAtIndex:pageControl.currentPage];

mainDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];

NSError * error;
NSURL * Tmpurl;
NSString *tmpContent;
NSString *urlAddress;

urlAddress = mainDelegate.TitleStrP1;
Tmpurl = [NSURL URLWithString:urlAddress];
tmpContent = [NSString stringWithContentsOfURL:Tmpurl encoding:NSUTF8StringEncoding error:&error];

maindelegate.content1 = [NSString stringWithString:tmpContent];

[controller.myWebView loadHTMLString:mainDelegate.content1 baseURL:nil]; 
}

And here what I am trying to do and what doesn't work. SomeFunction executes upon startup of the app. I checked the loaded value after downloading HTMLstring into content1 from the Internet site and html code indeed is there.

- (void)ReloadView:(int)page{
MyViewController *controller = [viewControllers objectAtIndex:pageControl.currentPage];
mainDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];

//code had been removed here

[controller.myWebView loadHTMLString:mainDelegate.content1 baseURL:nil]; 
}

-(void)SomeFunction{
NSError * error;
NSURL * Tmpurl;
NSString *tmpContent;
NSString *urlAddress;

urlAddress = mainDelegate.TitleStrP1;
Tmpurl = [NSURL URLWithString:urlAddress];
tmpContent = [NSString stringWithContentsOfURL:Tmpurl encoding:NSUTF8StringEncoding error:&error];

maindelegate.content1 = [NSString stringWithString:tmpContent];
}

Your help is appreciated. Thanks

EDIT:

AppDelegate.h

#import <UIKit/UIKit.h>

@class ContentController,MyViewController;

@interface AppDelegate : NSObject <UIApplicationDelegate>
{
    ...
    NSString *content1;
    NSString *content2;
}

...
@property (nonatomic, strong) NSString * content1;
@property (nonatomic, strong) NSString * content2;

AppDelegate.m

    - (void)applicationDidFinishLaunching:(UIApplication *)application
{
    content1 = [NSString stringWithString:@""];
    content2 = [NSString stringWithString:@""];

NSError * error;
NSURL * Tmpurl;
NSString *tmpContent;
NSString *urlAddress;

urlAddress = TitleStrP1;
Tmpurl = [NSURL URLWithString:urlAddress];
tmpContent = [NSString stringWithContentsOfURL:Tmpurl encoding:NSUTF8StringEncoding error:&error];
content1 = [NSString stringWithString:tmpContent];

urlAddress = TitleStrP2;
Tmpurl = [NSURL URLWithString:urlAddress];
tmpContent = [NSString stringWithContentsOfURL:Tmpurl encoding:NSUTF8StringEncoding error:&error];
content2 = [NSString stringWithString:tmpContent];
...
}
4

1 回答 1

0

appDelegate 中的“Content1”属性是否使用“strong”?此外,要使用属性,您应该使用较低的类,因为 setter 需要将变量名 (setContent1) 大写。那是

@property (nonatomic, strong) NSString *content1;

编辑:

[[UIApplication sharedApplication] delegate] 将始终有效。糟糕的是,您没有说那里是否有值(即,它是否为零)。所以其中之一正在发生。

1)您的 AppDelegate 对象正在被释放和释放。我不确定你会如何做到这一点,但它是可能的。

2)您已经告诉 UIApplication 在 main.m 中的“UIApplicationMain()”的第四个参数中创建一个委托,但还在您的 .plist 文件中指定了一个 NIB,并且该 nib 中也有一个 AppDelegate 对象并且已连接作为委托给 UIApplication。如果您使用的是 NIB,则 AppDelegate 的属性属性最好是 STRONG,因为它不被 UIApplication 保留(它在 UIApplication 的委托属性中说明了这一点)。

3) 如果“delegate”属性为 nil,则有人将其设置为 nil。

想一想 - UIApplication 委托很早就设置好了,否则你永远不会收到已启动的消息。

因此,您需要向您的应用程序委托添加一个“dealloc”方法并记录是否发生这种情况。您还可以添加一个简单的“init”方法,该方法还会在创建 AppDelegate 时记录 - 然后看看我的怀疑是否正确。

于 2012-07-30T23:52:23.603 回答