简体   繁体   中英

How to segue to a view controller based on a conditional statement?

I have a viewcontroller where I would like to check whether or not the user allows the app to use their location. If they accept then the app moves from the RootViewController to the OffersViewController if they deny then they should segue to the OffersNoGPSViewController.

At the moment it works if the user says "OK" if they say "Don't Allow" it remains on the RootViewController.

I'm also getting this "Unbalanced calls to begin/end appearance" warning when they move to the OffersViewController after touching "OK" and allowing the app to use their position.

thanks for the help. here's my code:

- (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");

    }
}

my full RootViewController code:

  @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

If the user does not allow location services, the location update method will never be called and your check for the status never happens. Try implementing the CLLocationManagerDelegate's locationManager:didChangeAuthorizationStatus: method. It gets called when the user chooses Yes or NO in the 'Allow Location Services' alert.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM