2

Well, that question sure sounds weird but i couldn't find a better way to put it.

I m pretty sure its a basic mistake but i m stuck.

I got a main home view controller, there are 2 buttons which leads to 2 different tableViewController.

i will use both of the selections.

But when i get the selected index from one table view and go the the next one, the first one's value become null.

if (tempFromLocationString!=NULL) {
    //tempFromLocationString=@"asd";
    fromLocationLabel.text=tempFromLocationString;
}
if (tempToLocationString!=NULL) {
    toLocationLabel.text=tempToLocationString;
}

this is how i segue from tableView to View controller

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{

    if ([[segue identifier] isEqualToString:@"fromLocationSegue"])
    {

         NSLog(@"%@",selectionString);
ViewController *vc = [segue destinationViewController];
        vc.tempFromLocationString=selectionString;


}
}

and this is how i get the selected cell's value.

- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{


   selectionString=[fromLocationArray objectAtIndex:indexPath.row];

    NSLog(@"%@",selectionString);


}

this is my code. i get temp strings with segues and i m applying these codes in view did load.

all the NSStrings declared in .h files.

the flow is like this;

user enter the app, select a button, goes to the first table view controller select a location, clicks ok button and goes back to the first view controller with segue ( selectionString) the label is set to the selectionString appropriately

user click next button, goes to the select table view select a location clicks ok and goes back the first view controller now the second label is set to the selectionString appropriately but now the first one is deleted and the string become null

2 Answers2

1

OK Your app flow

Case1

  1. User enter the app - Correct

  2. Select a button - Correct

  3. Goes to the First TableViewController select a location - Correct

  4. Clicks ok button - Correct

  5. and Goes back to the first view controller with segue (selectionString) the label is set to the selectionString appropriately - Incorrect

    Step 5 is incorrect, why?

    Answer - Because you are again pushing the ViewController after the selection in tableViewController, where as your ViewController already exist in the stack, so here instead of using segue, you should just pop the viewcontroller with same reference taken from ViewController.

Case2

  1. User click next button - Correct

  2. Goes to the select table view select a location clicks ok - Correct

  3. and goes back the first view controller now the second label is set to the selectionString appropriately but now the first one is deleted and the string become null - Incorrect

Step 3 is incorrect the same way as Case1.

Answer- Again you are actually not going back, you are going forward, so what happens is you are creating a new instance of ViewController on selection, which doesn't have the previous selected value.

Solution

  1. Create NSString property in each respective tableViewController separately same as you have in ViewController.

  2. When you segue tableViewController from ViewController, assign the property like

      TableViewController *vc = [segue destinationViewController];
      vc.tempFromLocationString=self.tempFromLocationString;
    
  3. On selection in tableviewcontroller do the following

      self.tempFromLocationString=selectionString;
      [self.navigationController popViewController:YES];
    
  4. Now instead of assigning value in ViewDidLoad in ViewController, do it in ViewWillAppear.

I hope it helps.

iphonic
  • 12,615
  • 7
  • 60
  • 107
  • i wasn't doing any segue process in view controller thats what i will try right now.Gonna let you know but thanks so much for your effort – user3790582 Jul 01 '14 at 09:39
0

Maybe your strings are not NULL when you set your labels.

Try to put a breakpoint before those lines, and check your temp strings

if (tempFromLocationString!=NULL) {
    //tempFromLocationString=@"asd";
    fromLocationLabel.text=tempFromLocationString;
}
if (tempToLocationString!=NULL) {
    toLocationLabel.text=tempToLocationString;
}

If they are not NULL try this:

if (tempFromLocationString && [tempFromLocationString length] > 0) {
    fromLocationLabel.text=tempFromLocationString;
}
tgyhlsb
  • 1,905
  • 14
  • 21