Xcode Interface Builder 未显示 App Delegate 对象
Posted
技术标签:
【中文标题】Xcode Interface Builder 未显示 App Delegate 对象【英文标题】:Xcode Interface Builder Not showing App Delegate Object 【发布时间】:2012-01-10 00:13:40 【问题描述】:我正在使用“ios 编程:Big Nerd Ranch 指南(第 2 版)自学 Objective-C 和 iOS 编程,我遇到了一个问题,教程希望我创建与 App Delegate 对象的连接,但是这个对象没有出现在我的界面生成器的对象列表中。我很确定它是一个错字,或者可能是一个不同的版本,因为这本书稍微落后于我的 Xcode 版本(4.2)。我已经包含了代码。我相当确定 MOCAppDelegate 对象应该在 IB 中显示,但我还不够熟悉,不知道我需要更改哪些代码才能实现这一点。具体问题:如何调整下面的代码以便获得一个对象IB 中的对象列表,以便我可以按照教程图形中的说明执行连接?
注意:我研究并发现了这个:Having trouble hooking up instance variables to AppDelegate 但这个解决方案对我不起作用(或者我没有正确实施)
头文件
#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
【问题讨论】:
【参考方案1】:将 NSObject 的一个实例拖到您的 .xib 中,然后按照您的说明将其拖放到 Objects 部分。然后选择它并在身份检查器中将其类型(在右侧,显示“自定义类”)更改为“MOCAppDelegate”(或您喜欢的任何类)。
【讨论】:
谢谢!大多数指南都跳过了这个基本步骤。 我也很高兴。那里有一些需要更新的文档。感谢 Stack Overflow 在您输入问题时显示类似的问题!【参考方案2】:您应该从对象库中将“NSObject”拖到您要连接的视图控制器下方黑条上的情节提要上。然后,单击 NSObject 并在身份检查器中将类更改为 AppDelegate。然后您可以创建与 AppDelegate 的连接。
【讨论】:
【参考方案3】:MOCAppDelegate = AppDelegate
代码是在您命名项目时为您生成的。 UIApplication 对象的委托名称通常根据项目名称略有不同。
(毫无疑问,任何印刷书籍都使用的是旧版 Xcode。)
选择 Files Owner
和 Identity Inspector (command-alt-2) 以确认文件所有者是应用委托的占位符。
【讨论】:
我做了这个更改,但 IB 没有更新。我需要做些什么来“刷新”它吗? When selecting The "File's Owner" Placeholder the Identity Inspector shows its Class as blank and the Identity dialogue has: Label:File's Owner, ObjectID: -1, Lock: Inherited - (Nothing), Notes:选中的复选框。我尝试将类更改为 MOCAppDelegate,但仍然没有在占位符下方的对象列表中获得代表委托的对象。以上是关于Xcode Interface Builder 未显示 App Delegate 对象的主要内容,如果未能解决你的问题,请参考以下文章
Xcode 的 Interface Builder 中的外部对象占位符丢失
在 iOS8 的 Interface Builder 中向 UITableView 添加静态单元格?内容部分未显示
Xcode 和 Interface Builder 随机停止对话