0

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

1 に答える 1

0

appDelegate の「Content1」プロパティは「strong」を使用していますか? また、プロパティを使用するには、セッターが変数名 (setContent1) を大文字にする必要があるため、下位クラスを使用する必要があります。あれは

@property (nonatomic, strong) NSString *content1;

編集:

[[UIApplication sharedApplication] delegate] は常に機能します。悪いことに、そこに値があるかどうか (つまり、nil かどうか) を言いませんでした。したがって、これらの1つが起こっています。

1) AppDelegate オブジェクトが解放され、割り当てが解除されています。これをどのように行うかはわかりませんが、可能です。

2) 「UIApplicationMain()」の 4 番目のパラメーターで、main.m にデリゲートを作成するよう UIApplication に指示しましたが、.plist ファイルで NIB も指定しており、その nib には AppDelegate オブジェクトがあり、配線されています。デリゲートとして UIApplication に。NIB を使用している場合、AppDelegate のプロパティ属性は、UIApplication によって保持されないため、STRONG にすることをお勧めします (これは、UIApplication のデリゲート プロパティで説明されています)。

3) "delegate" プロパティが nil の場合、誰かがそれを nil に設定しています。

考えてみてください。UIApplication デリゲートは非常に早い段階で設定されます。そうしないと、開始されたメッセージを取得できません。

そのため、アプリのデリゲートに「dealloc」メソッドを追加し、それが発生した場合はログに記録する必要があります。AppDelegate が作成されたときにもログに記録する単純な「init」メソッドを追加することもできます。その後、私の疑いが正しいかどうかを確認してください。

于 2012-07-30T23:52:23.603 に答える