简体   繁体   English

用于假人的Objective-C:如何使App Delegate与控制器通信?

[英]Objective-C for Dummies: How to make App Delegate communicate with Controllers?

how can I invoke methods in my controllers from within the app delegate. 如何从应用程序委托中调用控制器中的方法。 For example, my application uses the app delegate to monitor the user's location. 例如,我的应用程序使用应用程序委托来监视用户的位置。 After the location has been determined I want to call a method on a controller which has a MKMapView on it and show the user's location on it. 确定位置后,我要在控制器上调用一个方法,该方法上具有MKMapView并在其上显示用户的位置。 How can I go about doing that? 我该怎么做呢?

Here's my current code: 这是我当前的代码:

//  AppDelegate.m
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
    [[[DashboardViewController alloc] initWithNibName:@"DashboardViewController" bundle:nil] displayMyLocation:newLocation.coordinate];
}

Also tried this, which is crashing with SIGABRT , but is at least attempting to call the method: 还尝试过此操作,该操作使SIGABRT崩溃,但至少尝试调用该方法:

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
    [(DashboardViewController *)dashboardViewController displayMyLocation:manager];
}

Alternatively, now that I think about it, would it be wrong to have the controller also monitor the user's location and just self invoke it's methods? 或者,现在考虑一下,让控制器也监视用户的位置并仅自我调用它的方法是否是错误的? If I remember correctly having multiple CLLocationManagerDelegate delegates didn't affect performance because they all access the GPS identically? 如果我没记错的话,有多个CLLocationManagerDelegate委托不会影响性能,因为它们都以相同的方式访问GPS? Is that right or wrong? 是对还是错? It just kinda seems dumb to have multiple instances of the same thing... 拥有同一个事物的多个实例似乎有点愚蠢……

Thanks in advance! 提前致谢!

UPDATE for @nevan @nevan的更新

The map code: 地图代码:

- (void)displayMyLocation:(CLLocation *)location {
    MKCoordinateSpan span;
    MKCoordinateRegion region;

    span.latitudeDelta = 0.02;
    span.longitudeDelta = 0.02;

    region.center = location.coordinate;
    region.span = span;

    [map setRegion:region];
}

It would certainly be a good idea to have your controller monitoring for location events. 让您的控制器监视位置事件当然是个好主意。

A fairly decent alternative would be to use the notification center to notify the controller that a new location is available. 一个相当不错的选择是使用通知中心通知控制器新的位置可用。 I actually did exactly that in one of my applications. 实际上,我在其中一个应用程序中确实做到了这一点。 Check out line 70 of this file to see how to send a notification and line 54 of this file to see how tp subscribe tp these notifications . 检出此文件的第70行以了解如何发送通知,查看此文件的第54行以了解tp如何向这些通知发送tp

locationManager:didUpdateToLocation is called many times, each time the device gets a new fix on the location, so you'll end up allocing and initing your DashboardViewController many times. 每当设备在位置上获得新的修复时,都会多次调用locationManager:didUpdateToLocation ,因此您最终将需要多次分配和初始化DashboardViewController。 The proper way to do this would be to alloc and init a dashboardViewController instance variable when you set up your app delegate, then use that instance variable. 正确的方法是在设置应用程序委托时分配并初始化dashboardViewController实例变量,然后使用该实例变量。 It looks like you've done that in your second piece of code, but I don't understand why you have to cast it to be a DashboardViewController. 看起来您已经在第二段代码中完成了此操作,但是我不明白为什么您必须将其强制转换为DashboardViewController。

It would be better again to put the location manager into the same place as your mapview. 最好再将位置管理器放置在与地图视图相同的位置。 Why not move locationManager:didUpdateToLocation to there? 为什么不将locationManager:didUpdateToLocation移到那里?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM