Moving back from a Controller to a previous one

22,784

Solution 1

You don't push back. That creates a new instance of the previous controller class. You pop back. You can accomplish that 2 ways.

1: In code put in the following statement when you want to return (pop) to you tablet view controller.

[self.navigationController popViewControllerAnimated:YES];

2: If you want to do it in storyboard you need to implement the following custom segue class:

implementation

//  PopSegue.m

#import "PopSegue.h"

@implementation PopSegue

- (void) perform {

    UIViewController *src = (UIViewController *) self.sourceViewController;
[src.navigationController popViewControllerAnimated:YES];
}

and header

//  PopSegue.h

#import <UIKit/UIKit.h>

@interface PopSegue : UIStoryboardSegue

@end

Place this method in your UIViewController to set a property back to your UITableViewController:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue identifier] isEqualToString:@"goBackToTableView"])  {
    [[segue destinationViewController] set{whatEverProperyYouWantTo}: {valueOfPropertyToSet}];
    }
}

Solution 2

@T.J When adopt this method "2: If you want to do it in storyboard you need to implement the following custom segue class:

implementation"

and according to the document of the following, "Configuring the Destination Controller When a Segue is Triggered iOS performs the following tasks when a segue is triggered:

It instantiates the destination view controller. It instantiates a new segue object that holds all the information for the segue being triggered. Note: A popover segue also provides a property that identifies the popover controller used to control the destination view controller. It calls the source view controller’s prepareForSegue:sender: method, passing in the new segue object and the object that triggered the segue. It calls the segue’s perform method to bring the destination controller onto the screen. The actual behavior depends on the kind of segue being performed. For example, a modal segue tells the source view controller to present the destination view controller. It releases the segue object and the segue is complete."

It instantiates the presenting viewController once more, then dealloc after the overrid method of "perform" called. So It is better to choose the first method you submited or the methodof the delegate according the document.

Thanks

Share:
22,784
blaluma
Author by

blaluma

Updated on July 09, 2022

Comments

  • blaluma
    blaluma almost 2 years

    I'm trying to switch back from my UIViewController to my UITableViewController but apperendly it's not working the way I want it to. I did create my Interface with storyboards from the start, so I'm not really into moving from one View to another by code. Until now, I just pushed my Views per sergues which are easy to implement with storyboards. But when I tried moving back to my previous Views, a new ViewController will be implemented, so all my data I stored in the old one is "lost".

    Actually there is no code I could present you (because of the sergues) but the situation is:

    ->I got an MutableArray storing stuff in my UITableView.

    ->User taps the scan Button, scans a new Item which should be imported in my array.

    ->When trying to push back to my tableView there's a new Controller awating me, unaware of the data I stored in the old one.

    So how do I simply move back to my old Controller preventing to create a new one all the time?

    Does a sergue-push always creates a new Controller?

    (Question may be simple, but I'm new to this stuff. Tried some results presented by the search function, but none of them worked :( )

    /edit: Popback problem has been solved but array filling problem still exists. Code:

    GeneralSettings *sharedGS = [GeneralSettings sharedInstance];
    sharedGS.strEAN = [[NSString alloc] initWithString:strCheckString];
    [sharedGS.listArray insertObject:strCheckString atIndex:0];
    NSLog(@"Anzahl der EAN-Codes: %d\nErster Code: %@\n In Variable: %@", sharedGS.listArray.count, [sharedGS.listArray objectAtIndex:0],sharedGS.strEAN);
    

    Data in sharedGS.strEAN ("83274572354" i.e.) Data in listArray (null)

  • blaluma
    blaluma over 12 years
    Thanks! I'm using the first one because it would be a bit oversized to create a new class for a single pop back. BUT my array still isn't working as intended... I looked up my variable and my data is there ("432436274" for example) but when I try to add it to my array with an addObject or insertObject:atIndex cmd, theres nothing happening. Could you help me there too?
  • blaluma
    blaluma over 12 years
    Code looks like: 'GeneralSettings *sharedGS = [GeneralSettings sharedInstance]; sharedGS.strEAN = [[NSString alloc] initWithString:strCheckString]; [sharedGS.listArray insertObject:strCheckString atIndex:0]; NSLog(@"Anzahl der EAN-Codes: %d\nErster Code: %@\n In Variable: %@", sharedGS.listArray.count, [sharedGS.listArray objectAtIndex:0],sharedGS.strEAN);' Code formatting doesn't work or am I doing it wrong? 'MyCode' ?
  • T.J.
    T.J. over 12 years
    You could setup a delegate and delegate protocol, but a simpler way is to use the second option. The second way is pretty easy - just copy my code into a new objective-c class. Then in the UIViewController implement a prepareForSegue method and set a property on the destination view controller (your UITableViewController). I'll post the prepareForSegue method in may answer above.