編集 2
使用方法に依存することがわかりました。awakeFromNibではなくscrollViewDidEndDeceleratingを介してアクセスすると、appdelegateポインタが失われます。しかし、scrollViewDidEndDecelerating を使用する必要があります。あなたの誰かがこれを修正できますか?
正常に動作します
- (void)awakeFromNib
{
AppDelegate *mainDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
NSLog(@"%@", [NSString stringWithFormat:@"%@", mainDelegate.content1]);
}
mainDelegate.content1 への bad_access がクラッシュします
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
AppDelegate *mainDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
NSLog(@"%@", [NSString stringWithFormat:@"%@", mainDelegate.content1]);
}
元の質問(過去の参照用にこれを保持しています。上記の機能を読んでください)
この問題は stringWithContentsOfURL に関するものです
以下のコードは機能し、HTMLstring を mainDelegate.content1 にダウンロードして、UIWebView controller.myWebView に適切に表示します。関数を変更して、事前にダウンロード プロセスを実行するようにすると、それを AppDelegate.m のどこかに置くと、まさにこの関数で mainDelegate.content1 を処理するときにプログラムがクラッシュします。
ここに良いコードがあります
- (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];
}
そして、ここで私がやろうとしていることとうまくいかないこと。SomeFunction は、アプリの起動時に実行されます。インターネット サイトから HTMLstring を content1 にダウンロードした後、読み込まれた値を確認したところ、実際に HTML コードが存在します。
- (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];
}
あなたの助けに感謝します。ありがとう
編集:
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];
...
}