2012-01-10 40 views
6

Tôi đang dạy bản thân mình Objective-C và lập trình iOS với "IOS Lập trình: Hướng dẫn Big Nerd Ranch (2nd Edition) và tôi đã chạy vào một vấn đề mà các hướng dẫn muốn tôi để tạo ra các kết nối đối tượng App Delegate, nhưng đối tượng này không xuất hiện trong danh sách Objects trong bộ dựng Interface, tôi khá chắc chắn rằng nó là một lỗi đánh máy hoặc có lẽ là một phiên bản khác với cuốn sách hơi phía sau phiên bản Xcode của tôi (4.2). Tôi khá chắc chắn rằng các đối tượng MOCAppDelegate là những gì nên được hiển thị trong IB, nhưng tôi chưa đủ quen thuộc để biết những gì thay đổi mã tôi cần phải thực hiện điều đó xảy ra.Câu hỏi cụ thể: Làm thế nào để điều chỉnh mã dưới đây để tôi có được một đối tượng trong danh sách các đối tượng trong IB để tôi có thể thực hiện các kết nối theo hướng dẫn trong đồ họa hướng dẫn?Xcode Interface Builder Không hiển thị App Đại biểu Object

Lưu ý: Tôi nghiên cứu và thấy điều này: Having trouble hooking up instance variables to AppDelegate nhưng giải pháp này không làm việc cho tôi (hay tôi đã không thực hiện nó một cách chính xác)

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 

Thực hiện tập tin

#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 

Trả lời

14

Kéo một thể hiện của NSObject vào tệp .xib và thả nó vào phần Đối tượng giống như trong hướng dẫn bạn đã có. Sau đó chọn nó và thay đổi kiểu của nó trong thanh tra danh tính (ở bên phải, nơi nó nói "Custom Class") thành "MOCAppDelegate" (hoặc bất kỳ lớp nào bạn thích).

+3

Cảm ơn bạn! Bước cơ bản này đã được bỏ qua trong hầu hết các hướng dẫn. – pretzels1337

+1

Tôi cũng rất tuyệt vời. Có một số tài liệu ngoài kia cần được cập nhật. Kudos để Stack Overflow cho hiển thị các câu hỏi simliar trong khi bạn gõ của bạn! – McUsr

0

MOCAppDelegate = AppDelegate

c ode được tạo cho bạn khi bạn đặt tên cho dự án. Các đại biểu của đối tượng UIApplication thường được đặt tên hơi khác nhau tùy thuộc vào tên của dự án.

(Không có nghi ngờ rằng bất kỳ cuốn sách in đã sử dụng một Xcode cũ.)

Chọn Files Owner và Identity Inspector (command-alt-2) để xác nhận chủ sở hữu của tập tin là một giữ chỗ cho các đại biểu ứng dụng .

+0

Tôi đã thực hiện thay đổi này nhưng IB không cập nhật. Có điều gì tôi phải làm để "làm mới" nó? – Ketema

+0

Khi chọn Trình giữ chỗ "Chủ sở hữu tệp" Trình kiểm tra danh tính hiển thị Lớp của nó là trống và hộp thoại Nhận dạng có: Nhãn: Chủ sở hữu tệp, ObjectID: -1, Khóa: Kế thừa - (Không có gì), Ghi chú: hộp kiểm đã chọn. Tôi đã thử thay đổi lớp thành MOCAppDelegate, nhưng vẫn không nhận được một đối tượng đại diện cho đại biểu trong danh sách đối tượng bên dưới trình giữ chỗ. – Ketema

3

Bạn nên kéo một "NSObject" từ Object Library vào kịch bản của bạn trên thanh màu đen bên dưới bộ điều khiển xem bạn muốn kết nối. Sau đó, nhấp vào NSObject và trong trình kiểm tra danh tính, thay đổi lớp thành AppDelegate. Sau đó, bạn có thể tạo kết nối với AppDelegate.

Các vấn đề liên quan