Hiren
Hiren

Reputation: 12780

UInavigation controller did not work

I am making app like whenever the device change it's orientation mode it changes it's view.
At first when the view is portrait it shows perfect SearchViewController, then when i rotate to landscape it push to new view MapView in landscape ... now when i again change the view to portrait the map rotate to portrait... but it should be do to search view controller... and one more thing when i tapped detail disclosure button it should be go to back to search view controller... i think navigation controller not work in map view..

this is my code part of searchViewController

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
 {


    //return (interfaceOrientation == UIInterfaceOrientationPortrait);  
    if(interfaceOrientation == UIInterfaceOrientationPortrait|| inte rfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)  
    {  
    }  
    else {  
        MapView *mapView = [[MapView alloc] initWithNibName:@"MapView" bundle:nil];  
        mapView.title = @"Map";  
        [self.navigationController pushViewController:mapView animated:YES];   
        return YES;  

    }  
    return YES;

}  

and this is my MapView code

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation   {  
    // Return YES for supported orientations  
    //  return (interfaceOrientation == UIInterfaceOrientationLandscapeRight|| UIInterfaceOrientationLandscapeLeft);  
    if(interfaceOrientation == UIInterfaceOrientationPortrait|| interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)  
    {  
        NSLog(@"hi Potrait");  
        [self.navigationController popViewControllerAnimated:NO];  
        return YES;  
    }  
    else {  
    }  
    return YES;  
}

Upvotes: 0

Views: 132

Answers (1)

Deepak Danduprolu
Deepak Danduprolu

Reputation: 44633

shouldAutorotateToInterfaceOrientation: is an incorrect place to implement any kind of navigation. You should do it in didRotateFromInterfaceOrientation: instead. Check the current interfaceOrientation and push or pop if necessary.

In searchViewController,

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
    if ( UIInterfaceOrientationIsLandscape(self.interfaceOrientation) ) {
        MapView *mapView = [[MapView alloc] initWithNibName:@"MapView" bundle:nil];  
        mapView.title = @"Map";  
        [self.navigationController pushViewController:mapView animated:YES]; 
    }
}

In MapView,

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
    if ( UIInterfaceOrientationIsPortrait(self.interfaceOrientation) ) {
        [self.navigationController popViewControllerAnimated:NO];
    }
}

And alter your shouldAutorotateToInterfaceOrientation: to

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return YES;
} 

Upvotes: 1

Related Questions