Ios 将NSURL从一个类传递到另一个类
在didFinishLaunchingWithOptions中的我的appDelegate中,我有以下内容:Ios 将NSURL从一个类传递到另一个类,ios,cocoa-touch,Ios,Cocoa Touch,在didFinishLaunchingWithOptions中的我的appDelegate中,我有以下内容: NSURL *url = [NSURL URLWithString:@"http://www.thejenkinsinstitute.com/Journal/"]; NSString *content = [NSString stringWithContentsOfURL:url]; NSString * aString = content; NSMutableArray *substr
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];
- (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获取到类以加载它时,我做错了什么?在第二个代码中,只需更改赋值即可
- (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];
}
我从上面几行中了解到,您需要从某个类获取url,比如说
MyFirstViewControlle
,以及;之后,您尝试将该url保存在appDelegate
中,比如说MyAppDelegate
,这样您就可以在不同的控制器中使用相同的url,比如说,MyOtherViewController
。。。如果我的方向正确,那么请说明如何创建appDelegate的对象,它应该是这样的:(在您的MyFirstViewController中)
另外,还要确保在appDelegate中合成url属性的名称,如下所示:
@property(nonatomic,retain)NSURL *theURL;
@property(strong)NSURL *theURL;//ios 5.0
之后,由于您已经创建了委托对象,请执行以下操作:
objMyAppDelegate.theURL = _currentURL;
如果您还有任何疑问,请告诉我。这一行出现问题\u appdelegate.theheurl=currentURL
您正在用空字符串替换appdelegate url检查我编辑的答案,如果您有进一步的查询,请告诉我。它在控制台中仍然返回为空检查我编辑的答案它对我有效希望它能帮助您。
objMyAppDelegate.theURL = _currentURL;