Search code examples
iphoneuiviewcontrolleruinavigationcontroller

How to go back to previous view controller using custom back button?


I am new to iOS development. I am going to next screen using following statement.

locationViewController = [[LocationViewController alloc] initWithNibName:@"LocationViewController" bundle:nil];

[navigationController pushViewController:locationViewController animated:YES];

Now I want to go back to previous screen by tapping on custom back button on the top bar of the current screen.


Solution

  • use this:

    -(IBAction)back:(id)sender
    {
        [self.navigationController popViewControllerAnimated:YES];
    }
    

    For home button:

    NSArray *viewContrlls=[[self navigationController] viewControllers];
    for( int i=0;i<[ viewContrlls count];i++)
    {
        id obj=[viewContrlls objectAtIndex:i];
        if([obj isKindOfClass:[Rate_O_MeterViewController class]])
        {
            [[self navigationController] popToViewController:obj animated:YES];
            return;
        }
    }
    

    in place of Rate_O_MeterViewController give your controller name...add it in action of home button.