我不熟悉Objective-C和iOS编程,并且在过去3个小时内一直尝试使用xcode 5和iOS7 iPhone从头开始设置此gps / location应用。到目前为止,该应用已成功加载到我的iphone上。用户界面上有一个空标签和一个按钮。当我点击按钮时,它会显示“ YourGPSApp要使用您的当前位置”,然后显示“不允许”和“确定”选项。
但是,在选择任何一个选项之前,该窗口都会消失。我需要保留该窗口,并让用户选择一个选项。
到目前为止,这是我所做的:
在头文件中为IBOutlet UILabel对象创建了一个属性,并将其与用户界面连接。
在头文件中定义了IBAction方法,并将其与UI上的按钮连接。
这是我的头文件中的最终结果:
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController;
@property (nonatomic, strong) IBOutlet UILabel *gpsLabel;
-(IBAction)gpsButton;
@end
然后,我编辑了主文件,并得到了以下源代码:
#import "ViewController.h"
#import <CoreLocation/CoreLocation.h>
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(IBAction)gpsButton
{
CLLocationManager * gpsLM = [[CLLocationManager alloc]init];
[gpsLM startUpdatingLocation];
}
@end
此时,我有99%的把握确定,当我点击在标题中设置的gpsButton时,它成功询问用户iPhone的位置是因为我收到了窗口警报,询问我是否要允许它查看我的位置。
我还转到了我的iPhone设置,并手动为此应用程序打开了位置服务。然后,我返回到应用程序屏幕并再次按下按钮,它在iPhone屏幕右上角的击球手旁边显示了“查找位置”箭头,当我返回时,在我的应用程序名称旁边也显示了相同的箭头到我的iPhone的位置设置。
这告诉我我的“位置查找”方法可以正常工作。
我只需要弄清楚如何防止窗口消失,用户就可以选择“不允许”或“确定”。
谢谢您的帮助。
根据这一POST
原因是在确认选择之前已释放CLLocationManager。
您应该将CLLocationManager设置为成员,否则ARC将释放该对象,因此该窗口将自动关闭。
@property (nonatomic, strong) CLLocationManager * gpsLM;
-(IBAction)gpsButton
{
self.gpsLM = [[CLLocationManager alloc]init]; // you may like the alloc it in the init method
[self.gpsLM startUpdatingLocation];
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句