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。我在将 NSURL 从 AppDelegate 获取到类以加载它时做错了什么?

4

2 回答 2

0

在您的第二个代码中,只需更改分配值。

- (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,例如MyAppDelegate,这样您就可以在不同的控制器中使用相同的内容说,MyOtherViewController...如果我的方向正确,那么请说明您是如何制作的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 回答