2014-11-22 18 views
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 

回答

1

您已經有一個if語句來檢查在嘗試保存文件時是否有錯誤。
只需添加一個else說法,意思是成功保存的文件,顯示了UIAlertView

更改您的代碼如下:

編輯 - 由於UIAlertView在iOS 8的已過時,我已經修改了代碼,以便它可以在iOS 8和之前的版本上運行。

// Save string and check for error 
if (![newLocString writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:&error]) { 
    NSLog(@"An Error occurred: %@", error.localizedDescription); 
} else { 

    // Here we are receiving a string which holds the iOS version 
    NSString *iOSversion = [[UIDevice currentDevice] systemVersion]; 

    // Here we are comparing the above string, to the string "7.2" 
    // Since both are string, and not numbers, we are using the option 'NSNumericSearch' 
    // I've randomly chosen version 7.2, since iOS 7 latest version is 7.1.2, 
    // Every version number below 7.2 will be prior to iOS 8, and above 7.2 will be iOS 8 
    // If I would've chosen the latest iOS 7 version, which is 7.1.2, or the first iOS 8 version 
    // which is 8.0, I would have needed to add code which my comparison results will be NSOrderedSame 
    // Note that the comparison just gives me the order in which the below items will be, compared to one another 
    if([iOSversion compare:@"7.2" options:NSNumericSearch] == NSOrderedAscending) { 
     UIAlertView *savedAlert = [[UIAlertView alloc] initWithTitle:@"File saved" 
                  message:@"File saved successfully" 
                  delegate:nil 
                cancelButtonTitle:@"OK" 
                otherButtonTitles:nil]; 
     [savedAlert show]; 
    } 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]; 
    } 
} 
+0

謝謝,我做了這樣的事情,但像我一樣,我得到2警報,所以必須按兩次「OK」。我們用什麼IOS8? – Nicoll 2014-11-22 13:01:28

+0

您使用'UIAlertController',我將在第二秒內編輯我的代碼以包含'UIAlertController'的實現。 – AMI289 2014-11-22 13:06:53

+0

非常感謝您的幫助!所以我必須製作2個版本的IOS7和IOS8或將UIAlertController工作在IOS7中?任何想法爲什麼警報會出現兩次?哦,也許這是一次經度和緯度一次,你會知道如何解決這個問題嗎?再次感謝。 – Nicoll 2014-11-22 13:11:10

相關問題