2012-01-10 68 views
6

我在教自己Objective-C和iOS編程與「IOS編程:大書呆子牧場指南(第二版)」,我遇到了一個問題,其中教程希望我創建連接到一個App Delegate對象,但是這個對象並沒有出現在Interface builder的Objects列表中,對於我來說,我非常確定它是一個錯字還是一個版本,因爲這本書稍微落後於我的Xcode版本(4.2)。已經包含了代碼,我相當肯定MOCAppDelegate對象是應該在IB中顯示的,但我還不是很熟悉,不知道需要做什麼代碼更改。具體問題:如何調整代碼以便我可以在IB的Objects列表中獲得一個對象,以便我可以按照教程圖形中的說明執行連接?Xcode界面生成器不顯示應用程序委託對象

注:我研究並發現這一點:Having trouble hooking up instance variables to AppDelegate但這種方法並沒有爲我工作(或者我沒有正確地實現它)

ScreenShot 頭文件

#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 

實現文件

#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 

回答

14

將NSObject的一個實例拖放到.xib文件中,並將其放到Objects部分中,就像您獲得的說明一樣。然後選擇它並在身份檢查器(在右側,它表示「Custom Class」)將其類型更改爲「MOCAppDelegate」(或任何您喜歡的類)。

+3

謝謝!大多數指南都跳過了這一基本步驟。 – pretzels1337 2012-11-14 04:36:23

+1

我也很棒。有那些需要更新的索姆文件。榮譽堆棧溢出顯示模擬問題,而你鍵入你的! – McUsr 2013-11-28 18:44:21

0

MOCAppDelegate = AppDelegate

c當您爲項目命名時,ode是爲您生成的。根據項目的名稱,UIApplication對象的委託通常命名略有不同。

(毫無疑問,任何書籍的印刷中使用的是舊的Xcode。)

選擇Files Owner和身份檢查(命令-ALT-2),以確認該文件的所有者是應用程序委託的佔位符。

+0

我做了這個改變,但是IB沒有更新。有什麼我必須做的「刷新」它? – Ketema 2012-01-10 00:25:59

+0

當選擇「文件所有者」佔位符時,Identity Inspector將其類顯示爲空白,並且Identity對話框具有:標籤:文件的所有者,ObjectID:-1,鎖定:繼承 - (Nothing),註釋:複選框。我試着將類更改爲MOCAppDelegate,但仍然沒有在佔位符下方的對象列表中獲得代表委託的對象。 – Ketema 2012-01-10 00:40:32

3

您應該將對象庫中的「NSObject」拖動到您要連接的視圖控制器下方的黑色條上的故事板上。然後,單擊NSObject並在身份檢查器中將該類更改爲AppDelegate。然後您可以創建到AppDelegate的連接。

相關問題