开发者

How i call NSObject method in UIViewController class method?

I am the beginner in iphone development. I develop an application in which i calling some GPS related information(method name is getGPSInformation{}) in clsGPS{} is an pure NSObject class.The code is as follows,

#import "clsGPS.h"

-(void)getGPSInformation
{
    locationMa开发者_开发知识库nager = [[CLLocationManager alloc ] init];
    locationManager.delegate = self;
    locationManager.distanceFilter = 5.0f;
    locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;
    [locationManager startUpdatingLocation];    
}

I want the above method calling in UIViewController class. How i call this method in UIViewController class from that i automatically call this method at application launching time? should i calling that method in viewDidLoad event or viewWillAppear method?


The best place to put code you want to load at startup is the -applicationDidFinishLaunching: method of your application delegate. The application delegate is part of all projects created starting from one of the Xcode project templates.

One gotcha is the Default.png splash screen will persist until -applicationDidFinishLaunching: returns, so you might call your -getGPSInformation method with something like:

[self performSelector:@selector(getGPSInformation)
      withObject:nil
      afterDelay:0.1];

This will have the effect of putting the method call on the event queue, return immediately and call the location manager initialization after a tenth of a second.

If you wanted to call something when a view controller is loaded, -viewDidLoad would be a good choice. However a view controller can also be unloaded, so your locationManager object would be initialized (and potentially leaked, with the code above) multiple times. -viewDidLoad is usually used to tweak contents of the .xib interface file that has just been loaded, or things related to the UI.

-viewWillAppear is actually called every time your you navigate (forward or back) to the view controller, so that's definitely not what you want for the location manager.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜