Xcode Interface Builder 未显示应用程序委托对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8796977/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Xcode Interface Builder Not showing App Delegate Object
提问by Ketema
I am teaching myself Objective-C and iOS programming with "IOS Programming: The Big Nerd Ranch guide (2nd Edition) and I have run into an issue where the tutorial wants me to create connections to an App Delegate object, but this object does not appear in the Objects list in Interface builder for me. I am pretty sure its either a typo or perhaps a version different as the book is slightly behind my version of Xcode (4.2). I have included the code. I am fairly certain that the MOCAppDelegate object is what should be showing up in IB, but I am not yet familiar enough to know what code changes I need to make that happen. Specific question: How do I adjust the code below so that I get an object in the Objects list in IB so that I can perform the connections as instructed in the tutorial graphic?
我正在使用“IOS 编程:大书呆子牧场指南(第 2 版)”自学 Objective-C 和 iOS 编程,但我遇到了一个问题,教程希望我创建与 App Delegate 对象的连接,但该对象没有出现在我的界面构建器的对象列表中。我很确定它要么是错字,要么版本不同,因为这本书比我的 Xcode (4.2) 版本稍晚。我已经包含了代码。我很确定MOCAppDelegate 对象是应该在 IB 中显示的对象,但我还不够熟悉,无法知道需要进行哪些代码更改才能实现此目的。具体问题:如何调整下面的代码,以便在对象列表中获得一个对象在 IB 中,以便我可以按照教程图形中的说明执行连接?
Note: I researched and found this: Having trouble hooking up instance variables to AppDelegatebut this solution did not work for me (or I did not implement it correctly)
注意:我研究并发现了这一点:将实例变量连接到 AppDelegate 时遇到问题,但是这个解决方案对我不起作用(或者我没有正确实现它)
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
Implementation 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
回答by Caleb
Drag an instance of NSObject into your .xib and drop it in the Objects section just as in the instructions you've got. Then select it and change its type in the identity inspector (on the right side, where it says "Custom Class") to "MOCAppDelegate" (or whatever class you like).
将 NSObject 的一个实例拖到您的 .xib 中,然后按照您所获得的说明将其放入 Objects 部分。然后选择它并将其在身份检查器中的类型(在右侧,显示“自定义类”)更改为“MOCAppDelegate”(或您喜欢的任何类)。
回答by PopUp
You should drag an "NSObject" from the Object Library onto your storyboard on the black bar underneath the view controller you want to connect. Then, click on the NSObject and in the identity inspector change the class to AppDelegate. Then you can create the connection to the AppDelegate.
您应该将“NSObject”从对象库拖到您要连接的视图控制器下方的黑条上的故事板上。然后,单击 NSObject 并在身份检查器中将类更改为 AppDelegate。然后您可以创建到 AppDelegate 的连接。
回答by bshirley
MOCAppDelegate = AppDelegate
MOCAppDelegate = AppDelegate
The code was generated for you when you named the project. The delegate of the UIApplication object is usually named slightly different depending on the name of the project.
代码是在您命名项目时为您生成的。UIApplication 对象的委托通常会根据项目名称的不同而略有不同。
(There's no doubt that any book in print was using an older Xcode.)
(毫无疑问,任何印刷书籍都使用较旧的 Xcode。)
Select Files Owner
and the Identity Inspector (command-alt-2) to confirm the file's owner is a place holder for the app delegate.
选择Files Owner
和身份检查器 (command-alt-2) 以确认文件的所有者是应用程序委托的占位符。