如何根据条件语句转换到视图控制器?

时间:2013-01-29 14:44:03

标签: ios objective-c core-location cllocationmanager segue

我有一个viewcontroller,我想检查用户是否允许该应用使用其位置。如果他们接受,则应用程序从RootViewController移动到OffersViewController,如果他们拒绝,那么他们应该转到OffersNoGPSViewController。

如果用户说'#34; OK"如果他们说"不允许"它保留在RootViewController上。

我也得到了这个"不平衡的呼叫开始/结束外观"触摸" OK"触摸"然后移动到OffersViewController时发出警告并允许应用程序使用他们的位置。

感谢您的帮助。这是我的代码:

- (void)locationUpdate:(CLLocation *)location {
    locLabel.text = [location description];

    NSLog(@"auth status is %u", [CLLocationManager authorizationStatus]);

    if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorized) {
        [self performSegueWithIdentifier: @"SegueOffersGPS" sender: self];

    } else if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied)

    [self performSegueWithIdentifier: @"SegueOffersNoGPS" sender: self];


}

- (void)locationError:(NSError *)error {
    locLabel.text = [error description];
}


- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue identifier] isEqualToString:@"SegueOffersNoGPS"])
    {
        OffersViewController *vc = [segue destinationViewController];
        //vc.dataThatINeedFromTheFirstViewController = self.theDataINeedToPass;

        NSLog(@"auth status no GPS");
    }

    if ([[segue identifier] isEqualToString:@"SegueOffersGPS"])
    {
        OffersViewController *vc = [segue destinationViewController];


        //vc.dataThatINeedFromTheFirstViewController = self.theDataINeedToPass;


        NSLog(@"auth status is GPS");

    }
}

我的完整RootViewController代码:

  @interface RootViewController ()

@end

@implementation RootViewController
@synthesize CLController;


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

}

-(void) viewWillAppear:(BOOL)animated {

    CLController = [[CoreLocationController alloc] init];
    CLController.delegate = self;
    [CLController.locMgr startUpdatingLocation];

    NSLog(@"RootViewController");

    [CLController.locMgr startUpdatingLocation];
//    
//    if ([CLLocationManager authorizationStatus] ==3) {
//        [self performSegueWithIdentifier: @"SegueOffersGPS" sender: self];
//        
//        
//    }
//    if ([CLLocationManager authorizationStatus] !=3) {
//        [self performSegueWithIdentifier: @"SegueOffersNoGPS" sender: self];
//    }

}

-(void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {

    if (status == kCLAuthorizationStatusDenied) {
        // denied
        [self performSegueWithIdentifier: @"SegueOffersNoGPS" sender: self];
        [CLController.locMgr stopUpdatingLocation];


    }
    else if (status == kCLAuthorizationStatusAuthorized) {
        // allowed
        [self performSegueWithIdentifier: @"SegueOffersGPS" sender: self];
        [CLController.locMgr stopUpdatingLocation];
    }
}

- (void)locationUpdate:(CLLocation *)location {
    //locLabel.text = [location description];

    NSLog(@"auth status is %u", [CLLocationManager authorizationStatus]);

    if ([CLLocationManager authorizationStatus] ==3) {
        [self performSegueWithIdentifier: @"SegueOffersGPS" sender: self];

    }
    if ([CLLocationManager authorizationStatus] !=3) {
        [self performSegueWithIdentifier: @"SegueOffersNoGPS" sender: self];

    }      
}

- (void)locationError:(NSError *)error {

    [self performSegueWithIdentifier: @"SegueOffersNoGPS" sender: self];

}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue identifier] isEqualToString:@"SegueOffersNoGPS"])
    {
        self.navigationController.toolbarHidden=YES;

        NSLog(@"auth status no GPS");
    }

    if ([[segue identifier] isEqualToString:@"SegueOffersGPS"])
    {
        self.navigationController.toolbarHidden=NO;

        NSLog(@"auth status is GPS");

    }
}

-(void)viewDidDisappear:(BOOL)animated {

    [CLController.locMgr stopUpdatingLocation];

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

1 个答案:

答案 0 :(得分:2)

如果用户不允许位置服务,则永远不会调用位置更新方法,也不会检查状态。尝试实现CLLocationManagerDelegate的locationManager:didChangeAuthorizationStatus:方法。当用户在“允许位置服务”警报中选择“是”或“否”时,它会被调用。