How do I go back to the previous view controller using a custom back button?

I am new to iOS development. I will proceed to the next screen using the following instruction.

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

[navigationController pushViewController:locationViewController animated:YES];

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

+3


source to share


3 answers


use this:

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

      

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;
    }
}

      

replace Rate_O_MeterViewController with the name of your controller ... add it to your home button action.

+9


source


Create Back Button:

    UIBarButtonItem * backButton=[[UIBarButtonItem alloc]initWithTitle:@"Back" style:UIBarButtonItemStyleDone target:self action:@selector(goBack:)];
    self.navigationItem.leftBarButtonItem=backButton;

      



And write the function:

-(void)goBack:(id)sender
 {
      [self.navigationController popViewControllerAnimated:YES]
 }

      

0


source


Try it,

appDelegate.m - didFinishLaunchingWithOptions method

self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController" bundle:nil] autorelease];
//-----------navigation view--------------
UINavigationController *navHomeController = [[UINavigationController alloc] initWithRootViewController:self.viewController];
self.window.rootViewController = navHomeController;

      

0


source







All Articles