How can I alternate view controller and not lose data

I have two UIViewControllers, one is a UIPickerViewController, the Other a UITableViewController. Ideally the Picker should get a request from the user to add x amount of some item to the tableView. The Picker gets user inputs and assigns them to variables val1, val2, val3, where val1 is the number of items (number of rows) and val2 is the name or label for the item.

PickerViewController.m

- (IBAction)add:(id)sender
{

   TableViewController *tvc = [[TableViewController alloc] init];

    [tvc setValues:self.val1 :self.val2 :self.val3];
    [self presentViewController:tvc animated:YES completion:nil];
}

TableViewController.m

-(void)setValues:(NSString *)newVal1 :(NSString *)newVal2 :(NSString *)newVal3
{
    self.val1 = newVal1;
    self.val2 = newVal2;
    self.val3 = newVal3;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"UITableViewCell"];

    // This is just a header which holds my "Add" button
    UIView *header = self.headerView;
    [self.tableView setTableHeaderView:header];

    [self addNew:self.val1 :self.val2 :self.val3];

}

- (void)addNew:(NSString *)newVal1 :(NSString *)newVal2 :(NSString *)newVal3
{  
    if(!self.numberOfRows){
        NSLog(@"Initially no of rows = %d", self.numberOfRows);
        self.numberOfRows = [self.val1 intValue];
        NSLog(@"Then no of rows = %d", self.numberOfRows);
    }
    else
    {
        self.numberOfRows = self.numberOfRows + [newVal1 intValue];
        NSLog(@"New no rows = %d", self.numberOfRows);
    }

    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:self.numberOfRows inSection:0];

    // Only run when called again .. not initially
    if(self.run != 0){

        [self.tableView beginUpdates];
        [self.tableView insertRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationBottom];
        self.run ++;
        [self.tableView endUpdates];
    }   
}

// "ADD" button which should go back to the picker and get new items to add to the table
- (IBAction)testAdd:(id)sender
{
    PickerViewController *pvc = [[PickerViewController alloc] init];
    [self presentViewController:pvc animated:YES completion:nil];

}

Now, I realize every time I call the next view controller I am creating a new instance of it, but I don't know how else to do it, I figure this is the main problem. As of right now, I expect when I leave the tableview for the picker view and return the console should log "New no of rows = x" but that doesn't happen.

I know val3 isn't used and my addNew: may not be the best, but I just need it to handle the basic logging mentioned above and I should be able to take it from there.

Been stuck on this for days

Answers


Create a property for TableViewController, and only create it the first time you present it,

- (IBAction)add:(id)sender {
    if (! self.tvc) {
    self.tvc = [[TableViewController alloc] init];
    }
    [self.tvc setValues:self.val1 :self.val2 :self.val3];
    [self presentViewController:self.tvc animated:YES completion:nil];
}

It's not entirely clear from you question, whether it's this presentation or the one you have in the table view class that you're talking about. It also looks like you're doing something wrong in terms of presentation -- you're presenting the picker view from the table view controller, and also presenting the table view controller from the picker. That's not correct, you should present which ever controller you want to appear second, and that controller should use dismissViewControllerAnimated to go back, not present another controller.


In testAdd you don't need to create a new instance and present it. If you want to go back to the presentingViewController, just use dismissViewControllerAnimated .

And you will go one controller up in the stack.


Need Your Help

ionic ion-nav-buttons ng-if not work for first load

javascript angularjs ionic-framework ionic

I have a table view, when I click one cell will call my PixCtrl controller. If I check connection work, I will get data by $http.get, if not I will use sqlite. This is work. I want my ion-nav-butto...

How to read a file into a Java Bitmap?

java android file-io bitmap bytearray

I know how to read a bitmap file into a byte array. How is the byte array then converted to a Java Bitmap?

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.