在ios中没有从应用程序委托中调用方法

在ios中没有从应用程序委托中调用方法

本文介绍了在ios中没有从应用程序委托中调用方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从应用程序的其他地方调用方法,以获取在应用程序委托中获取的用户位置。从另一个视图控制器调用 CLLocation * getCurLoc = [AppDelegate getCurrentLocation]; 时,不会返回任何内容。

I want to call a method from elsewhere in the app to get the user's location that was obtained in the app delegate. When calling CLLocation *getCurLoc = [AppDelegate getCurrentLocation]; from another view controller, nothing is returned.

应用程序代表是,

@synthesize locationManager;

CLLocation *location;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{

    locationManager = [[CLLocationManager alloc]init];
    locationManager.delegate = self;
    locationManager.desiredAccuracy=kCLLocationAccuracyKilometer;
    [locationManager startUpdatingLocation];

    return YES;
}

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{

    [locations lastObject];
    [manager stopUpdatingLocation];

    CLLocation *location =  [locations lastObject];
}

+(CLLocation *)getCurrentLocation{
    return location;
}

将其更改为具有 - 的实例方法无效。应该将位置做成一个实例吗?代理人是否应该成为一个实例,还是有更好的方式从其他地方访问?

Changing it to an instance method with a "-" didn't work. Should "location" be made into an instance? Should the delegate be made into an instance, or is there a better way to access it from elsewhere?

推荐答案

是以一种与您的方法相一致的方式解决此问题的有效快捷方式 - 将位置存储在 appDelegate

Martin's answer is an effective quick&dirty way of solving this problem in a way that is consistent with your approach - storing the location in appDelegate.

如果您想进一步了解,您可能需要考虑实现一个特定的对象来保存数据 - 数据模型。在应用程序委托中存储数据是一种不好的做法 - 它不是那里的(尽管它在样本或小应用程序中工作得很好)。

If you want to take a step further you might want to consider implementing a special object that would hold the data - the data model. It is considered a bad practice to store data in application delegate - it is not what it is there for (though it works perfectly fine in sample or small applications).

你可以这样做:

DataModel.h

#import <Foundation/Foundation.h>

@interface DataModel : NSObject

@property (strong) CLLocation *location;

+ (DataModel *)sharedModel;

@end

DataModel.m / p>

DataModel.m

#import "DataModel.h"

@class CLLocation;

@implementation DataModel

- (id) init
{
    self = [super init];
    if (self)
    {
        _location = nil;
    }
    return self;
}

+ (DataModel *)sharedModel
{
    static DataModel *_sharedModel = nil;
    static dispatch_once_t onceSecurePredicate;
    dispatch_once(&onceSecurePredicate,^
                  {
                      _sharedModel = [[self alloc] init];
                  });

    return _sharedModel;
}

@end

然后您需要 #importDataModel.h无论你需要什么。您将更改您的 didUpdateLocations:到:

You would then need to #import "DataModel.h" wherever you need it. You would change your didUpdateLocations: to:

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    [locations lastObject];
    [manager stopUpdatingLocation];

    [DataModel sharedInstance].location = [locations lastObject];
}

从代码中的任何地方,您可以通过 [DataModel sharedInstance] .location

And from anywhere in the code you could get this location simply by [DataModel sharedInstance].location.

编辑

对于一个非常简单的应用程序,这种方法可能看起来像一个过度的杀手。但是一旦你的应用程序成长,它肯定会付出代价。

For a very simple app this approach might look as an overkill. But as soon as your app grows it surely pays off to use it.

这种类/对象/单例是保存你的应用程序所需的所有数据(固定的和临时)。所以所有的数据源可以很好地利用它。简而言之:它使您能够轻松地遵循模型视图控制器指南。

This kind of class/object/singleton is ment to hold all the data your app needs (fixed and temporary). So all the data sources can make a good use of it. In short: it enables you to easily follow the model-view-controller guidelines.

你当然使用另一个类/对象/单例来保存临时数据 - 它取决于您的数据结构的复杂性。

You cold of course use another class/object/singleton to hold the temporary data - it depends on the complexity of your data-structure.

您不必具体初始化此类对象。第一次引用时初始化它。这就是为什么 dispatch_once 。它确保存在此共享对象的唯一一个实例:

You don't have to specifically initialize this kind of object. It is initialized the first time you reference it. That is why dispatch_once is there for. It makes sure that there is one and only one instance of this shared object present: http://stackoverflow.com/a/9119089/653513

这个 [DataModel sharedInstance] 的单个实例将一直保留在您的应用程序终止之前

And this one single instance of [DataModel sharedInstance] will remain there until your app is terminated.

Apple为 [NSUserDefaults standardDefaults],[UIApplication sharedApplicaton] 使用类似的方法。

Apple uses similar approach for [NSUserDefaults standardDefaults], [UIApplication sharedApplicaton] for example.

我倾向于将 #importDataModel.h放入我的项目 Prefix.pch 所以我不必每次使用它导入它(它被用于所有的应用程序)。

I tend to put the #import "DataModel.h" into my projects Prefix.pch so I don't have to import it every single time I use it (it is used trough all the app).

PROS:
- 整个应用程序中的数据可用
- 代码可重用性
- MVC结构
- 代码更易读

PROS:- data accesible throughout the app- code reusability- MVC structure- code is more readable

CONS:
- 我找不到一个。除了 dispatch_once(& onceSecurePredicate,^ ... 可能会在头几秒内混淆一个。

CONS:- I couldn't really find one. Except that the dispatch_once(&onceSecurePredicate,^... might confuse one for the first couple of seconds.

这篇关于在ios中没有从应用程序委托中调用方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 06:27