微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

objective-c – Xcode Interface Builder未显示App Delegate对象

我正在通过“ IOS编程:大书呆子牧场指南(第2版)”自学Objective-C和iOS编程,我遇到了一个问题,教程要求我创建与App Delegate对象的连接,但是这个对象没有出现在“接口”构建器的“对象”列表中.对于我来说,我非常确定它是一个拼写错误,也可能是版本不同,因为这本书略微落后于我的 Xcode版本(4.2).我已经包含了代码.我相当确定MOCAppDelegate对象应该在IB中出现,但我还不熟悉,知道我需要做什么代码更改.具体问题:如何调整下面的代码,以便在Objects列表中获取一个对象在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

解决方法

将NSObject的一个实例拖到你的.xib中,然后将其放入Objects部分,就像你已经掌握的那样.然后选择它并在身份检查器(右侧,它显示自定义类”)中将其类型更改为“MOCAppDelegate”(或您喜欢的任何类).

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐