0

didFinishLaunchingWithOptions の appDelegate には、次のものがあります。

NSURL *url = [NSURL URLWithString:@"http://www.thejenkinsinstitute.com/Journal/"];
NSString *content = [NSString stringWithContentsOfURL:url];
NSString * aString = content;
NSMutableArray *substrings = [NSMutableArray new];
NSScanner *scanner = [NSScanner scannerWithString:aString];
[scanner scanUpToString:@"<p>To Download the PDF, " intoString:nil]; // Scan all characters before #
while(![scanner isAtEnd]) {
    NSString *substring = nil;
    [scanner scanString:@"<p>To Download the PDF, <a href=\"" intoString:nil]; // Scan the # character
    if([scanner scanUpToString:@"\"" intoString:&substring]) {
        // If the space immediately followed the #, this will be skipped
        [substrings addObject:substring];
    }
    [scanner scanUpToString:@"" intoString:nil]; // Scan all characters before next #
}
// do something with substrings

NSString *URLstring = [substrings objectAtIndex:0];
self.theheurl = [NSURL URLWithString:URLstring];
NSLog(@"%@", theheurl);
[substrings release];

theheurl のコンソール出力には、.pdf で終わる有効な URL が表示されます。

URLをロードしたいクラスには、次のものがあります。

- (void)viewWillAppear:(BOOL)animated
{
_appdelegate.theheurl = currentURL;
NSLog(@"%@", currentURL);
NSLog(@"%@", _appdelegate.theheurl);
[worship loadRequest:[NSURLRequest requestWithURL:currentURL cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60.0]];
timer = [NSTimer scheduledTimerWithTimeInterval:(1.0/2.0) target:self selector:@selector(tick) userInfo:nil repeats:YES];
[super viewWillAppear:YES];

}

ただし、そのクラスの両方の NSLog は null に戻ります。AppDelegate からクラスに NSURL を取得してロードする際に何が間違っていますか?

4

2 に答える 2

0

2番目のコードで、割り当て値を変更するだけです。

- (void)viewWillAppear:(BOOL)animated
{

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

  currentURL=appdell.theheurl;
NSLog(@"%@", currentURL);
NSLog(@"%@", appdell.theheurl);
[worship loadRequest:[NSURLRequest requestWithURL:currentURL cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60.0]];
timer = [NSTimer scheduledTimerWithTimeInterval:(1.0/2.0) target:self selector:@selector(tick) userInfo:nil repeats:YES];
[super viewWillAppear:YES];

}
于 2012-11-24T05:09:10.950 に答える
0

上記の行から、いくつかのクラスからURLを取得する必要があることを理解していますMyFirstViewControlle。その後、その URL を に保存しようとすると、別のコントローラーで同じものを使用できるようになります。appDelegateたとえばMyAppDelegateMyOtherViewController... 正しい方向に進んでいる場合は、 の作成方法を明確にしてください。次のようにobject of appDelegateなります。 (あなたのMyFirstViewController)

MyAppDelegate *objMyAppDelegate = (MyAppDelegate*)[[UIApplication sharedApplication] delegate];

また、次のように、appDelegate で url プロパティを合成することも忘れないでください。

@property(nonatomic,retain)NSURL *theURL;
@property(strong)NSURL *theURL;//ios 5.0

その後、すでにデリゲート オブジェクトを作成しているので、次のようにします。

objMyAppDelegate.theURL = _currentURL;

さらに質問があれば教えてください。

于 2012-11-24T05:20:42.083 に答える