2012-11-24 32 views
0

在我didFinishLaunchingWithOptions的appDelegate,我有以下几点:通NSURL从一类到另一个

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

} 

然而,在课内外NSLogs回来空。我在从AppDelegate获取NSURL到类中加载它时做错了什么?

+0

此行使得问题'_appdelegate.theheurl = CURRENTURL;'你是一个空字符串 –

+0

检查我的编辑答案,让你有进一步的查询我知道更换的appdelegate网址。 –

回答

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

} 
+0

它仍然是回来为空控制台 – user717452

+0

检查我的编辑答案它为我希望它会帮助你。 –

0

我从你的上述内容中了解到,你需要从某个类获得网址,例如MyFirstViewControlle;之后您尝试保存的网址在您的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; 

告诉我,如果你再有任何查询。