Having trouble hooking up instance variables to AppDelegate

╄→尐↘猪︶ㄣ 提交于 2019-12-25 14:48:42

问题


Am having some trouble hooking up instance variables in the visual object editor using Xcode4.

Have been able to connect the Whereami App Delegate to the mapView and activityIndicator, but for some reason, can't find the locationTitleField. Am also having trouble connecting the delegates back to the App Delegate.

What am I doing wrong?

Here is the code for 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:


Try making them properties instead of 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

and don't forget to synthesize them

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

I never use iVars for objects that appear in nib files; I tend to always use properties and have never experienced any issues with hooking up the outlets.



来源:https://stackoverflow.com/questions/6500215/having-trouble-hooking-up-instance-variables-to-appdelegate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!