在iOS中,该方法未从应用程序委托调用。

13 浏览
0 Comments

在iOS中,该方法未从应用程序委托调用。

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

应用程序委托如下:

@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;
}

将其更改为实例方法并使用"-"并不起作用。是否应该将"location"变为实例?是否应该将委托变为实例,或者是否有更好的方法从其他位置访问它?

0
0 Comments

问题的原因是在iOS中没有从应用程序委托调用的方法。解决方法是通过访问[UIApplication sharedApplication].delegate来访问AppDelegate,然后调用所需的方法。为了避免每次都需要进行强制类型转换和访问[UIApplication sharedApplication].delegate,可以在应用程序委托中实现一个静态方法sharedDelegate,然后通过[[YourAppDelegateClassName sharedDelegate] getCurrentLocation]来调用所需的方法。

这个问题的解决方法是在应用程序委托中实现一个静态方法sharedDelegate,然后通过[[YourAppDelegateClassName sharedDelegate] getCurrentLocation]来调用所需的方法。这样可以避免每次都需要进行强制类型转换和访问[UIApplication sharedApplication].delegate的操作。

这个解决方法的好处是可以方便地在应用程序的任何地方访问AppDelegate,并且可以通过静态方法来调用所需的方法,而不需要每次都进行强制类型转换和访问[UIApplication sharedApplication].delegate

以上是关于(method not called from app delegate in ios)问题出现的原因以及解决方法的整理。这个问题的解决方法是在应用程序委托中实现一个静态方法sharedDelegate,然后通过[[YourAppDelegateClassName sharedDelegate] getCurrentLocation]来调用所需的方法。这样可以方便地在应用程序的任何地方访问AppDelegate,并且可以避免每次都需要进行强制类型转换和访问[UIApplication sharedApplication].delegate的操作。

0
0 Comments

问题出现的原因:

此问题的原因是在iOS应用程序中没有从App Delegate调用方法。在给定的代码示例中,`didUpdateLocations:`方法没有被从App Delegate调用。

解决方法:

为了解决这个问题,可以使用以下方法:

1. 创建一个专门的对象来保存数据 - 数据模型。这是一种良好的做法,因为将数据存储在应用程序委托中被认为是一种不好的做法。可以创建一个名为`DataModel`的类来实现此功能。

2. 在`DataModel`类中,使用`sharedModel`方法创建一个单例对象,以确保只有一个实例存在。

3. 在`didUpdateLocations:`方法中,通过`[DataModel sharedInstance].location`将位置数据存储在`DataModel`对象中。

4. 在应用程序的其他任何地方,可以通过`[DataModel sharedInstance].location`获取位置数据。

这种方法的优点是数据在整个应用程序中都可以访问,代码可以重用,符合MVC架构,代码更易读。缺点是可能会对`dispatch_once`方法的使用感到困惑。

使用这种方法可以解决问题,并且在应用程序增长时也会有好处。

0
0 Comments

在iOS开发中,有时候会遇到"method not called from app delegate"的问题。该问题出现的原因是在`didUpdateLocations`方法中,定义并设置了一个局部变量`location`,而没有使用文件顶部定义的全局变量`location`。将以下代码:

CLLocation *location = [locations lastObject];

修改为:

location = [locations lastObject];

可以解决该问题。但更好的解决方法是在`AppDelegate`类中使用属性。可以在类的扩展中定义该属性:

@interface AppDelegate ()
@property (strong, nonatomic) CLLocation *location;
@end

然后在实例方法中可以通过以下方式访问该属性:

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
    [manager stopUpdatingLocation];
    self.location = [locations lastObject];
}

在类方法中可以通过以下方式访问该属性:

+(CLLocation *)getCurrentLocation {
    AppDelegate *app = [[UIApplication sharedApplication] delegate];
    return app.location;
}

如果`AppDelegate`声明了符合某个协议(可以在公共接口或类扩展中声明),例如:

@interface AppDelegate () 
@property (strong, nonatomic) CLLocation *location;
@end

那么上述代码会出现警告:

initializing 'AppDelegate *__strong' with an expression of incompatible type 'id'

此时需要进行显式的类型转换:

+(CLLocation *)getCurrentLocation {
    AppDelegate *app = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    return app.location;
}

在使用`AppDelegate *app = [[UIApplication sharedApplication] delegate];`时,会出现警告`initializing 'AppDelegate *__strong' with an expression incompatible with type 'id'`。通过结合gimenetes的答案,可以使用以下代码解决该问题:

+ (AppDelegate *)sharedDelegate {
    return (AppDelegate *)[UIApplication sharedApplication].delegate;
}

在测试和编写答案时,我没有收到警告,但是我找到了你的情况下为什么需要进行类型转换,并已更新答案。

0