2012-01-10 19 views
6

Mi sto insegnando Programmazione Objective-C e iOS con "Programmazione IOS: la guida di Big Nerd Ranch (2a edizione) e ho riscontrato un problema in cui il tutorial mi chiede di creare connessioni a un oggetto Delega app, ma questo oggetto non è presente nell'elenco Oggetti in Interface builder per me. Sono abbastanza sicuro che sia un errore di battitura o forse una versione diversa in quanto il libro è leggermente indietro rispetto alla mia versione di Xcode (4.2). ho incluso il codice. Sono abbastanza certo che l'oggetto MOCAppDelegate è ciò che dovrebbe essere mostrato in IB, ma non sono ancora abbastanza familiare da sapere quali modifiche al codice devo fare per realizzarlo. Domanda specifica: Come aggiusto il codice di seguito in modo che ottenga un oggetto nell'elenco Oggetti in IB in modo da poter eseguire le connessioni come indicato nel grafico tutorial?Xcode Interface Builder Non mostra oggetto Delega app

Nota: ho ricercato e trovato questo: Having trouble hooking up instance variables to AppDelegate ma questa soluzione non ha funzionato per me (o non mi ha attuato in modo corretto)

ScreenShot Header File

#import <UIKit/UIKit.h> 
#import <CoreLocation/CoreLocation.h> 
#import <MapKit/MapKit.h> 

@interface MOCAppDelegate : UIResponder <UIApplicationDelegate, CLLocationManagerDelegate> 
{ 
    CLLocationManager *locationManager; 

    IBOutlet MKMapView *worldView; 
    IBOutlet UIActivityIndicatorView *activityIndicator; 
    IBOutlet UITextField *locationTitleField; 
} 

@property (strong, nonatomic) IBOutlet UIWindow *window; 


@end 

Attuazione File

#import "MOCAppDelegate.h" 

@implementation MOCAppDelegate 

@synthesize window = _window; 

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    //Create location manager object 
    locationManager = [[CLLocationManager alloc] init]; 
    [locationManager setDelegate:self]; 

    //We want all results from the location manager 
    [locationManager setDistanceFilter:kCLDistanceFilterNone]; 

    //And we want it to be as accurate as possible 
    //regardless of how much time/power it takes 
    [locationManager setDesiredAccuracy:kCLLocationAccuracyBest]; 

    //Tell our location manager to start looking for it location immediately 
    [locationManager startUpdatingLocation]; 

    //We also want to know our heading 
    if (locationManager.headingAvailable == true) { 
     [locationManager startUpdatingHeading]; 
    } 


    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 
    // Override point for customization after application launch. 
    self.window.backgroundColor = [UIColor darkGrayColor]; 
    [self.window makeKeyAndVisible]; 
    return YES; 
} 

- (void)locationManager:(CLLocationManager *)manager 
    didUpdateToLocation:(CLLocation *)newLocation 
      fromLocation:(CLLocation *)oldLocation 
{ 
    NSLog(@"%@", newLocation); 
} 

- (void)locationManager:(CLLocationManager *)manager 
     didUpdateHeading:(CLHeading *)newHeading 
{ 
    NSLog(@"%@", newHeading); 
} 

- (void)locationManager:(CLLocationManager *)manager 
     didFailWithError:(NSError *)error 
{ 
    NSLog(@"Could not find location: %@", error); 
} 

- (void)dealloc 
{ 
    if([locationManager delegate] == self) 
     [locationManager setDelegate:nil]; 
} 

- (void)applicationWillResignActive:(UIApplication *)application 
{ 
    /* 
    Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. 
    Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. 
    */ 
} 

- (void)applicationDidEnterBackground:(UIApplication *)application 
{ 
    /* 
    Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
    If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 
    */ 
} 

- (void)applicationWillEnterForeground:(UIApplication *)application 
{ 
    /* 
    Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background. 
    */ 
} 

- (void)applicationDidBecomeActive:(UIApplication *)application 
{ 
    /* 
    Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. 
    */ 
} 

- (void)applicationWillTerminate:(UIApplication *)application 
{ 
    /* 
    Called when the application is about to terminate. 
    Save data if appropriate. 
    See also applicationDidEnterBackground:. 
    */ 
} 

@end 

risposta

14

Trascina un'istanza di NSObject nel tuo .xib e rilasciatela nella sezione Oggetti come nelle istruzioni che hai. Quindi selezionalo e cambia il suo tipo nell'Ispettore identità (a destra, dove si dice "Custom Class") a "MOCAppDelegate" (o qualsiasi altra classe tu voglia).

+3

Grazie! Questo passaggio fondamentale è stato saltato nella maggior parte delle guide. – pretzels1337

+1

Anche io sono bravo. Ci sono alcuni documenti là fuori che devono essere aggiornati. Complimenti allo Stack Overflow per mostrare domande simliar mentre scrivi il tuo! – McUsr

0

MOCAppDelegate = AppDelegate

Il c ode è stato generato per te quando hai nominato il progetto. Il delegato dell'oggetto UIApplication viene in genere denominato leggermente diverso a seconda del nome del progetto.

(Non c'è dubbio che qualsiasi libro in stampa stava usando un Xcode più vecchio.)

Selezionare Files Owner e l'Ispettore Identity (comando-alt-2) per confermare il proprietario del file è un segnaposto per il delegato app .

+0

Ho apportato questa modifica ma l'IB non è stato aggiornato. C'è qualcosa che devo fare per "rinfrescarlo"? – Ketema

+0

Quando si seleziona il segnaposto "Proprietario del file", Identity Inspector mostra la sua classe come vuota e la finestra di dialogo Identità ha: Etichetta: Proprietario file, ID oggetto: -1, Blocco: Ereditato - (Niente), Note: una casella di controllo selezionata. Ho provato a cambiare la classe in MOCAppDelegate, ma ancora non ottengo un oggetto che rappresenta il delegato nella lista degli oggetti sotto i segnaposto. – Ketema

3

È necessario trascinare un "NSObject" dalla libreria degli oggetti sullo storyboard sulla barra nera sotto il controller della vista che si desidera connettere. Quindi, fai clic su NSObject e nella finestra di ispezione della identità cambia la classe in AppDelegate. Quindi puoi creare la connessione a AppDelegate.

Problemi correlati