无法将实例变量连接到AppDelegate

时间:2011-06-27 23:51:19

标签: iphone objective-c interface-builder xcode4

enter image description here使用Xcode4在可视对象编辑器中连接实例变量时遇到一些问题。

已经能够将Whereami App Delegate连接到mapView和activityIndi​​cator,但由于某种原因,找不到locationTitleField。我也无法将代表连接回App Delegate。

我做错了什么?

以下是Whereami App Delegate.h的代码:

#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
#import <MapKit/MapKit.h>

@interface WhereamiAppDelegate : NSObject     <UIApplicationDelegate,CLLocationManagerDelegate> {
    UIWindow *window;
    CLLocationManager *locationManager;
    IBOutlet MKMapView *mapView;
    IBOutlet UIActivityIndicatorView *activityIndicator;
    IBOutlet UITextView *locationTitleField;
}

@property (nonatomic, retain) IBOutlet UIWindow *window;

@end

Whereami App Delegate.m

#import "WhereamiAppDelegate.h"

@implementation WhereamiAppDelegate


@synthesize window=_window;

- (BOOL)application:(UIApplication *)application     didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.
    //-- Create location manager object --
    locationManager = [[CLLocationManager alloc] init];

    //-- Make this instance of WhereamiAppDelegate the delegate
    //-- It will sends its messages to our Whereami delegate.
    [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 its location
    //-- immediately
    [locationManager startUpdatingLocation];
    [self.window makeKeyAndVisible];
    return YES;
}

- (void)locationManager:(CLLocationManager *)manager
   didFailWithError:(NSError *)error
{
NSLog(@"Could not find location: %@", error);
 }

- (void)locationManager:(CLLocationManager *)manager 
didUpdateToLocation:(CLLocation *)newLocation 
       fromLocation:(CLLocation *)oldLocation
{
    NSLog(@"%@", newLocation);
}

1 个答案:

答案 0 :(得分:0)

尝试制作属性而不是iVars ......

@interface WhereamiAppDelegate : NSObject <UIApplicationDelegate,CLLocationManagerDelegate>

@property (nonatomic, retain) IBOutlet CLLocationManager *locationManager;
@property (nonatomic, retain) IBOutlet MKMapView *mapView;
@property (nonatomic, retain) IBOutlet UIActivityIndicatorView *activityIndicator;
@property (nonatomic, retain) IBOutlet UITextView *locationTitleField;
@property (nonatomic, retain) IBOutlet UIWindow *window;

@end

并且不要忘记合成它们

@synthesize locationManager = _locationManager;
@synthesize mapView = _mapView;
@synthesize activityIndicator = _activityIndicator;
@synthesize locationTitleField = _locationTitleField;

我从未将iVars用于nib文件中出现的对象;我倾向于总是使用房产,从来没有遇到任何问题与挂钩。