- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我是 objective-c 的新手,过去 2 个半小时一直在努力解决这个问题。到目前为止,我已经取得了一些成功,我的应用程序可以在应用程序启动后立即开始搜索用户的位置。
然后我根据这个建议为 CLLocationManager
设置了一个委托(delegate):
“添加声明此类 (ViewController.h/m) 正在采用此特定协议(protocol)的语法。默认情况下,创建此类实例的任何其他类也将自动采用该协议(protocol)。
@interface ViewController : UIViewController <CLLocationManagerDelegate>
上面列出的代码行显示了用于显示 ViewController 采用 CLLocationManagerDelegate 协议(protocol)的语法。我们只需将它添加到标题中 @interface 行的尖括号 <> 之间。”
所以我成功地将上面的行添加到我的 ViewController.m 和 ViewController.h 文件中,然后我也遵循了相同的教程建议:
“完整的方法是:
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
并且在实现时它会告诉委托(delegate)人新的位置数据可用。我们对这个方法有两个论点。第一个让您知道哪个 CLLocationManager 提供了更新,最后一个提供了存储在 NSArray 中的 CLLocation 信息。”
下面是我所有的 ViewController.h
代码,接下来是我的 ViewController.m 代码:
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
@interface ViewController : UIViewController <CLLocationManagerDelegate>
@property (nonatomic, strong) IBOutlet UILabel *gpsLabel;
-(IBAction)gpsButton;
@end
这是我的 ViewController.m 代码:
#import "ViewController.h"
#import <CoreLocation/CoreLocation.h>
@interface ViewController () <CLLocationManagerDelegate>
@property (nonatomic, strong) CLLocationManager * gpsLM;
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations;
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.gpsLM = [[CLLocationManager alloc]init];
[self.gpsLM startUpdatingLocation];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(IBAction)gpsButton{
}
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
}
@end
我对从这里去哪里感到困惑。假设到目前为止我什至正确地做所有事情(是吗?),那么我如何访问存储在名为 locations 的 NSArray 对象中的位置数据?
谢谢你的帮助。
最佳答案
欢迎来到 iOS 社区!
首先,您可能会发现查看 CLLocationManagerDelegate 会很有帮助引用,在 locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
方法的标题下。这里重要的一点是:
locations: An array of CLLocation objects containing the location data. This array always contains at least one object representing the current location. If updates were deferred or if multiple locations arrived before they could be delivered, the array may contain additional entries. The objects in the array are organized in the order in which they occurred. Therefore, the most recent location update is at the end of the array.
重要的是,当用户的位置发生变化时,系统会调用此方法。
因此,例如,当位置更新时,您可以通过将您的实现更改为类似以下内容来向您的控制台打印一条消息:
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
CLLocation *currentLocation = [locations lastObject];
NSLog(@"Location is: %.5f %.5f", currentLocation.coordinate.latitude, currentLocation.coordinate.longitude);
}
如果你想做一些事情来响应用户事件,你可以使用 manager.location
,它会在每次 CLLocationManager 检测到新位置时自动更新。 (如果不存在,您需要向您的类添加另一个实例变量以存储最近的位置,并在 locationManager:didUpdateLocations: 中更新它。)
因此,例如,如果您想在按下按钮时使用当前位置更新您的标签,您可以添加如下内容:
-(IBAction)gpsButton {
CLLocation *currentLocation = self.gpsLM.location;
self.gpsLabel.text = [NSString stringWithFormat:@"Location is: %.5f, %.5f"];
}
(注意:这假定 gpsButton 操作和 gpsLabel socket 已连接到 Interface Builder 中的图形化内容。)
如果您熟悉其他编程语言,这就是推与拉的区别。 CLLocationManager 提供了一个push模型(它调用你的locationManager:didUpdateLocations:
方法来立即通知你变化),还有一个pull模型(你可以随时通过 .location
向它询问最新位置)。您使用哪一个取决于您想要做什么。
关于ios - 需要访问包含 CLLocation 信息的 NSArray 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20715152/
在我的应用程序中,我必须创建这样的结构: $arrProducts = array( array( “product_id” => “1”, “qty” =>
我相信我的问题涉及指针,这是一个我经常纠结的概念,但这就是我正在尝试做的事情。 我有六个 NSArray。我想要一个由这六个数组组成的额外 NSArray,所以: self.arr1 = [NSArr
我正在使用 VKSdk 制作 iOS 应用程序.我想将数据从 response.json[@"items"](它是 nsarray,response.json 是它的 nsdictionary)复制到
我创建 NSArray *test = @[@[@1,@2,@3,@4], @[@3,@5,@6,@7]]; 这将被视为 nsarray 的 nsarray。 但是当我想用打印出来的时候 NSLog
首先引用下图: 我有一个包含 9 个元素的 NSArray。这 9 个元素中的每一个都包含另外 5 个元素。 我想做的是将 [*][1] 从每个地方带到另一个地方,只包含这些日期。 如何最好地实现这一
我正在尝试将 NSDictionaries 数组排序为基于 NSDictionary valueForKey: 第一个字母之一的字母化数组数组 实际上我想要看起来有点像这样的东西 Array A //
我有一个 NSArray 输出如下: ( { category = 1; categoryname = Random; excuse = "Enter your exc
我是 Objective-C 和 iPhone 的新手 我必须使用值对 NSDictionary 进行排序,并且根据我必须排列该值的键,我已经对数组进行了排序。 //getting values NS
我觉得这很简单,但我就是无法理解它。我有一组星期几供用户选择: NSArray * daysOfTheWeek = [NSArray arrayWithObjects:@"sunday", @"mon
我有一个 NSArray 对象: class Object { var name: String? = nil var id: String? = nil } 我想创建一个具有唯一“名
我只是想将 NSNumbers 的 NSArray 排序为数字顺序,但我不确定最好的方法。以我的思维方式,001 和 002 具有相当的可比性,所以我怀疑两者都可以。对于 003,我不确定当方法需要
我面临一个非常常见的场景。 我有一个 NSArray,它有一个自定义类型的对象,比如 Person。 Person 类具有以下属性:firstName、lastName 和 age。 如何从具有 Pe
我有一个 NSArray,其中填充了大约 30 个 NSDate 格式的日期, 我需要做的是从中创建另一个数组,其中包含从第一个日期到最后一个日期的所有日期的 bool 值。 前数组 111/1/12
NSarray 的简单问题。我将对象存储在 NSMuteableArray 中。[obj1, obj2, obj3] 如果选择了一个对象,我想把它放在数组的前面。 IE。如果选择了 obj3,则: [
我已按字母顺序存储联系人,因此我有联系人列表的数组数组,该数组已显示在屏幕上。现在我想通过姓名搜索联系人,但谓词在这里无法正常工作。我已经完成了下面的代码。 filterArray.filter
对于使用 ARC 的 iOS 5.0 应用程序,我有一个对象的 NSArray,其中包含其他对象的 NSArray。是否可以在不遍历数组的情况下从内部数组中提取对象列表,例如比如,使用 NSPredi
这个问题在这里已经有了答案: Sort NSArray of custom objects based on sorting of another NSArray of strings (5 个答案
我有一个 NSArray 对象,每个对象都包含一个“键”字段 我有另一个带有嵌套NSArrays的NSArray,通过不同的字段“按字段分组”对这些对象进行分组,例如显示名称(字母分组) 例如 @in
我正在尝试在多个 NSArray 中搜索和合并对象。 基本上 NSArray 具有多个 NSArray 和 1 个对象,然后是 NSDictionary。我想检查该对象是否已经存在并将其与现有对象合并
这个问题在这里已经有了答案: Check that the contents of one NSArray are all in another array (9 个回答) 关闭 8 年前。
我是一名优秀的程序员,十分优秀!