Domanda

Ho associato la mia app con un UTI in modo che gli utenti possano avviare gli allegati KML. Nella app iPad delegato della mia app universale, posso vedere le launchOptions e da questi ottengo un NSURL per il file in fase di lancio. Voglio conservare questo come un modo globale che io possa accedere da altrove nella mia app, sto facendo questo con un Singleton chiamato motore. Questo è il mio App delegato:

- (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;
}

Il mio motore sembra di classe come questo:

//  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

Il mio problema è che quando provo ad accedere alla variabile LaunchFile dal Motore altrove nella mia app (da un controller di vista) gli spettacoli debugger il valore di Engine.launchFile di essere. Io sono l'accesso alla variabile in questo modo:

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

Qualsiasi aiuto?

È stato utile?

Soluzione

Il tuo codice sembra OK a prima vista - si può impostare un punto di interruzione myEngine.launchFile = solo per vedere cosa MyEngine sta puntando? Questo dovrebbe fare in modo che il codice Singleton sta lavorando.

Inoltre, hai verificato che [launchOptions valueForKey: @ "UIApplicationLaunchOptionsURLKey"] restituisce sicuramente un oggetto? Cercavi di digitare questo:

[launchOptions valueForKey:UIApplicationLaunchOptionsURLKey];

Sam

P Si dovrebbe leggere la risposta a questa domanda sulla creazione di oggetti Singleton, ci sono alcune sostituzioni che si dovrebbe mettere nella vostra classe del motore;)

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top