0
如何添加僅在成功將我的座標保存到文本文件時纔會顯示的UIAlertView,非常感謝。如何在成功保存文件後添加UIAlertView
我剛剛更新了所有代碼&的帖子,想知道是否可以看到它顯示兩次警報視圖的原因。
謝謝。
#import "ViewController.h"
@interface ViewController()
@end
@implementation ViewController {
CLLocationManager *locationManager;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
locationManager = [[CLLocationManager alloc] init];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)getCurrentLocation:(id)sender {
locationManager.delegate = (id)self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];
}
#pragma mark - CLLocationManagerDelegate
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(@"didFailWithError: %@", error); UIAlertView *errorAlert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Failed to Get Your Location" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; [errorAlert show];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
NSLog(@"Location updated: %@", newLocation);
CLLocation *currentLocation = newLocation;
if (currentLocation != nil) {
_LatitudeLabel.text = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude];
_LongitudeLabel.text = [NSString stringWithFormat:@"%.6f", currentLocation.coordinate.longitude];
_GPSAccuracyLabel.text = [NSString stringWithFormat:@"%.2f", currentLocation.horizontalAccuracy];
_AltitudeLabel.text = [NSString stringWithFormat:@"%.2f", currentLocation.altitude];
_VerticalAccuracyLabel.text = [NSString stringWithFormat:@"%.2f", currentLocation.verticalAccuracy];
}
NSString *newLocString = [NSString stringWithFormat:@"%s%f\n%s%f","Lat=",currentLocation.coordinate.latitude,"Long=",currentLocation.coordinate.longitude];
NSString *path = @"var/mobile/Documents/location.txt";
NSError *error = nil;
// Save string and check for error
if (![newLocString writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:&error]) {
NSLog(@"An Error occurred: %@", error.localizedDescription);
} else {
UIAlertView *savedAlert = [[UIAlertView alloc] initWithTitle:@"File saved"message:@"File saved successfully" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[savedAlert show];
}
/* IOS8
else {
// Create the alert itself
UIAlertController *savedAlert = [UIAlertController alertControllerWithTitle:@"File saved" message:@"File saved successfully" preferredStyle:UIAlertControllerStyleAlert];
// Create the "OK" button 'Action'
UIAlertAction *defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {}];
// Add the 'Action' created above, to our alert (otherwise, we won't have any button in it)
[savedAlert addAction:defaultAction];
// Presents the alert
[self presentViewController:savedAlert animated:YES completion:nil];
}
======================================
FOR IOS 7
else {
UIAlertView *savedAlert = [[UIAlertView alloc] initWithTitle:@"File saved"message:@"File saved successfully" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[savedAlert show];
}
*/
// Stop Location Manager
[locationManager stopUpdatingLocation];
}
@end
謝謝,我做了這樣的事情,但像我一樣,我得到2警報,所以必須按兩次「OK」。我們用什麼IOS8? – Nicoll 2014-11-22 13:01:28
您使用'UIAlertController',我將在第二秒內編輯我的代碼以包含'UIAlertController'的實現。 – AMI289 2014-11-22 13:06:53
非常感謝您的幫助!所以我必須製作2個版本的IOS7和IOS8或將UIAlertController工作在IOS7中?任何想法爲什麼警報會出現兩次?哦,也許這是一次經度和緯度一次,你會知道如何解決這個問題嗎?再次感謝。 – Nicoll 2014-11-22 13:11:10