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