持续启动nsurl作为全局变量
-
29-09-2019 - |
题
我已经将我的应用程序与UTI联系起来,以便用户可以启动KML附件。在我的通用应用程序的iPad应用程序委托中,我可以看到启动器,从这些启动器中,我得到了启动文件的nsurl。我想将其作为全局存储,以便可以在应用程序中的其他地方访问它,我正在使用名为Engine的单例进行此操作。这是我的应用程序委托:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
Engine *myEngine=[Engine sharedInstance];
StormTrackIpad *newVC=[[StormTrackIpad alloc] initWithNibName:@"StormTrackIpad" bundle:nil];
[window addSubview:newVC.view];
NSURL *launchFileURL=(NSURL *)[launchOptions valueForKey:@"UIApplicationLaunchOptionsURLKey"];
myEngine.launchFile=launchFileURL;
/* Show details of launched file
NSString *message =launchFileURL.absoluteString;
NSString *title = [NSString stringWithFormat:@"Opening file"];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:message delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[alert release];
*/
[window makeKeyAndVisible];
return YES;
}
我的引擎课看起来像这样:
// Engine.h
#import <Foundation/Foundation.h>
@interface Engine : NSObject {
NSURL *launchFile;
}
+ (Engine *) sharedInstance;
@property (nonatomic, retain) NSURL *launchFile;
@end
// Engine.m
#import "Engine.h"
@implementation Engine
@synthesize launchFile;
static Engine *_sharedInstance;
- (id) init
{
if (self = [super init])
{
// custom initialization
}
return self;
}
+ (Engine *) sharedInstance
{
if (!_sharedInstance)
{
_sharedInstance = [[Engine alloc] init];
}
return _sharedInstance;
}
@end
我的问题是,当我尝试从应用程序(从视图控制器)中其他位置的引擎访问启动文件变量时,调试器显示了Engine.launchfile的值。我正在访问这样的变量:
- (void)viewDidLoad {
[super viewDidLoad];
Engine *myEngine=[Engine sharedInstance];
NSURL *launchFile=myEngine.launchFile;
NSString *title = [NSString stringWithFormat:@"Opening file"];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:launchFile.absoluteString delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[alert release];
}
有帮助吗?
解决方案
乍一看,您的代码看起来还不错 - 您可以在myEngine.launchfile =只是为了查看MyEngine指向的内容吗?这应该确保您的单例代码正常工作。
另外,您是否检查了[lainingoptions valueforkey:@“ uiapplicationlaunchoptionsurlkey”]肯定会返回对象?您的意思是要输入以下内容:
[launchOptions valueForKey:UIApplicationLaunchOptionsURLKey];
山姆
p你应该阅读 回答这个问题 在创建单身对象时,您应该将一些覆盖物放入引擎类中;)
不隶属于 StackOverflow