How to use push Storyboard Segue for the NavigationController

33,967

Solution 1

First of all, get rid of the second navigation controller like what everybody here are saying.

Then, in storyboard, connect the "+" button directly to Add Customer View Controller and set the segue as push.

Next, click on the navigation bar of Customer view controller, in the attributes inspector where you should have defined the title ("Customer") for this view, there should be a "Back Button" row. Type "Back" and you will get a Back button in Add Customer view controller.

In prepareForSegue,

if([segue.identifier isEqualToString:@"AddCustomer"])
{    
    AddCustomerViewController *addCustomerViewController = segue.destinationViewController;
    addCustomerViewController.delegate = self;
}

To close Add Customer View controller, use popViewControllerAnimated as follows:

-(void) addCustomerViewControllerDidSave: (AddCustomerViewController *) Controller newCustomer: (Customer *) customer
{
    [self.customers addObject:customer];
    [self.tableview reloadData];
    [self.navigationController popViewControllerAnimated:YES];
}

Solution 2

You only need one navigation controller. Try delete the second navigation controller and set the segue from your first table directly to your second one.

Edit:

I took a more careful look and I think you should use popViewControllerAnimated rather than dismissModalViewControllerAnimated. The latter is for modal, the former is for push.

Share:
33,967
Ali
Author by

Ali

Updated on July 05, 2022

Comments

  • Ali
    Ali almost 2 years

    I am new in iOS development. I created an App using Storyboard to navigate to different pages.

    enter image description here

    When I click on the Add BarButton in Customer page --> go to the Add Customer Page.(using Modal Storyboard Segue)

    There, when I enter username and password and click on save button --> come back to Customer page.

    And everything works fine.

    The problem is I want to have back button in Add Customer page. I already know that I can use Push Storyboard Segue, but when I use it I face with an error:

    Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Pushing a navigation controller is not supported'
    *** First throw call stack:
    (0x13cb022 0x155ccd6 0xeff1b 0xefa24 0x44bde6 0x4404d0 0x13cce99 0x1814e 0x256a0e 0x13cce99 0x1814e 0x180e6 0xbeade 0xbefa7 0xbe266 0x3d3c0 0x3d5e6 0x23dc4 0x17634 0x12b5ef5 0x139f195 0x1303ff2 0x13028da 0x1301d84 0x1301c9b 0x12b47d8 0x12b488a 0x15626 0x25ed 0x2555)
    terminate called throwing an exception(lldb) 
    

    CustomerViewController.h:

    #import "AddCustomerViewController.h"
        @interface CustomerViewController : UITableViewController<AddCustomerViewControllerDelegate>
        {
            IBOutlet UITableView *tableview;
            NSMutableArray *customers;
        }
    
        @property(nonatomic,retain)IBOutlet UITableView *tableview;
        @property(nonatomic,retain)NSMutableArray *customers;
    
        @end
    

    This is the code I use in CustomerViewController.m:

    self.customers = [[NSMutableArray alloc]init]; 
    
    -(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
    {
        if([segue.identifier isEqualToString:@"AddCustomer"])
        {
            UINavigationController *navigationController = segue.destinationViewController;
            AddCustomerViewController *addCustomerViewController = [[navigationController viewControllers] objectAtIndex:0];
            addCustomerViewController.delegate = self;
        }
    }
    
    -(void) addCustomerViewControllerDidSave: (AddCustomerViewController *) Controller newCustomer: (Customer *) customer
    {
        [self dismissViewControllerAnimated: YES completion:NULL];
        [self.customers addObject:customer];
        [self.tableview reloadData];
    }
    

    AddCustomerViewController.h:

    #import "Customer.h"
    
    @class AddCustomerViewController;
    
    @protocol AddCustomerViewControllerDelegate <NSObject>
    
    -(void) addCustomerViewControllerDidSave: (AddCustomerViewController *) Controller newCustomer: (Customer *) customer;
    
    @end
    
    @interface AddCustomerViewController : UITableViewController
    {
    
    }
    
    @property (nonatomic, weak) id <AddCustomerViewControllerDelegate> delegate;
    
    @property (nonatomic, strong) IBOutlet UITextField *firstnameTxt;
    @property (nonatomic, strong) IBOutlet UITextField *lastnameTxt;
    
    - (IBAction)save:(id)sender;
    
    @end
    

    And AddCustomerViewController.m:

    - (void)save:(id)sender 
    {
        NSLog(@"Save");
        Customer *newCustomer = [[Customer alloc]init];
        newCustomer.firstname = self.firstnameTxt.text;
        newCustomer.lastname = self.lastnameTxt.text;
        [self.delegate addCustomerViewControllerDidSave:self newCustomer:newCustomer];
    
    }
    

    Can you help me how can I use Push Storyboard Segue (to have back button) ?

  • Ali
    Ali almost 12 years
    I did that but I got an error also on that way. I will post the error for you.
  • danqing
    danqing almost 12 years
    is that for popViewControllerAnimated?
  • Ali
    Ali almost 12 years
    Thanks, But how can I use popViewControllerAnimated? I don't know where did I use dismissModalViewControllerAnimated!
  • danqing
    danqing almost 12 years
    First line in your addCustomerViewControllerDidSave. Sorry dismissModalViewControllerAnimated is the same as dismissViewControllerAnimated.
  • Ali
    Ali almost 12 years
    This is the new error with popViewControllerAnimated : Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Pushing a navigation controller is not supported' *** First throw call stack: (0x13cb022 0x155ccd6 0xeff1b 0xefa24 0x44bde6 0x4404d0 0x13cce99 0x1814e 0x256a0e 0x13cce99 0x1814e 0x180e6 0xbeade 0xbefa7 0xbe266 0x3d3c0 0x3d5e6 0x23dc4 0x17634 0x12b5ef5 0x139f195 0x1303ff2 0x13028da 0x1301d84 0x1301c9b 0x12b47d8 0x12b488a 0x15626 0x25bd 0x2525) terminate called throwing an exception(lldb)
  • Ali
    Ali almost 12 years
    I just used it like this: [self.navigationController popViewControllerAnimated:YES]
  • danqing
    danqing almost 12 years
    are you still having two navigation controllers? Can you post updated code? What superclass do your two tableview controllers have?
  • Ali
    Ali almost 12 years
    I removed the second navigationController. I added all my code to the end of question. Still it doesn't work. the error is what I commented here.
  • Ali
    Ali almost 12 years
    I got where error happen : if([segue.identifier isEqualToString:@"AddCustomer"]) the prblem is when I remove this line I can go to seccond page but by click on save button I don't come back to customer page.
  • danqing
    danqing almost 12 years
    Can you update your code for CustomerViewController.m too? You need to set the second tableview controller as your destination, as you've removed the second navController. You also need to change dismissViewController into popViewController since you are using push.
  • Ali
    Ali over 11 years
    Sorry for my delay to reply. Yes your code is completely working.
  • Ali
    Ali over 11 years
    Hi, Sorry for my late reply. I found you answer useful and I appreciate for your help. @Rick codes solved the problem.