-2

Just a little question. I have a root view controller (AufnahmeIstTableViewController) and a detail view controller (AufnahmeISTDetailTableViewController)

I also have an int called istAufnahmeInt. When the user selects row 1 on my root view controller, then the value of my int is set to 0. When row 2 is pressed the value is set to 1, ... .

In my prepareForSegue method, I have the following code:

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if([segue.identifier isEqualToString:@"DetailView"])
{
AufnahmeISTDetailTableViewController *controller =(AufnahmeISTDetailTableViewController *)segue.destinationViewController;
controller.istAufnahmeInt = //What should I enter here? ;
}
}

So, what do I have to enter where my comment is to get the value of my istAufnahmeInt?

I use Storyboards.

For completion: Here's my didSelectRowAtIndexPath method:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
AufnahmeISTDetailTableViewController *categories =     [[AufnahmeISTDetailTableViewController alloc]init];

if ([[self.categoryArray objectAtIndex:indexPath.row] isEqual:@"Gesetze"])
categories.istAufnahmeInt = 0;

if ([[self.categoryArray objectAtIndex:indexPath.row] isEqual:@"Messmittel"])
categories.istAufnahmeInt = 1;

if ([[self.categoryArray objectAtIndex:indexPath.row] isEqual:@"Dritte"])
categories.istAufnahmeInt = 2;

[categories setTitle:[self.categoryArray objectAtIndex:indexPath.row]];


}
  • Are you trying to set the value of `istAufnahmeInt` on your view controller before the segue, or retrieve the value *from* the view controller? – Zev Eisenberg Jun 12 '14 at 17:14
  • possible duplicate of [Passing Data Between View Controllers DidSelectRowsAtIndexPath Storyboards](http://stackoverflow.com/questions/16380092/passing-data-between-view-controllers-didselectrowsatindexpath-storyboards) – Fruity Geek Jun 12 '14 at 17:16
  • No that's not a duplicate: He wants to know how to pass the data and I want to know how to pass an integer – user3734506 Jun 12 '14 at 17:18
  • Passing an integer is passing data, just a more specific example of it. – tophyr Jun 12 '14 at 17:21

2 Answers2

0

Your didSelectRowAtIndexPath: method isn't actually doing anything - it allocates and initializes a AufnahmeISTDetailTableViewController, but then does nothing with that controller so it goes away at the end of that method.

What you want to do is set the istAufnahmeInt value on the controller you have in prepareForSegue:, based on the currently selected row of your tableView - basically, put the logic you have in didSelectRowAtPath: into prepareForSegue:.

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    if([segue.identifier isEqualToString:@"DetailView"])
    {
        AufnahmeISTDetailTableViewController *controller = (AufnahmeISTDetailTableViewController *)segue.destinationViewController;
        if ([[self.categoryArray objectAtIndex:[self.tableView indexPathForSelectedRow].row] isEqual:@"Gesetze"])
            controller.istAufnahmeInt = 0;

        if ([[self.categoryArray objectAtIndex:[self.tableView indexPathForSelectedRow].row] isEqual:@"Messmittel"])
            controller.istAufnahmeInt = 1;

        if ([[self.categoryArray objectAtIndex:[self.tableView indexPathForSelectedRow].row] isEqual:@"Dritte"])
            controller.istAufnahmeInt = 2;

        [controller setTitle:[self.categoryArray objectAtIndex:[self.tableView indexPathForSelectedRow].row]];
    }
}
tophyr
  • 1,658
  • 14
  • 20
  • I'm sorry, but I don't know what to enter in my prepareforsegue method. Could you please help me? :) I'm sorry, but I'm new to development – user3734506 Jun 12 '14 at 17:28
  • The code I pasted should work exactly as-is for your `prepareForSegue:` method, as long as your tableview is called `tableView`. If it's called something else, you'll have to change `tableView` to whatever the variable name is, but aside from that this should be what you need. – tophyr Jun 12 '14 at 17:34
  • Thank you really really much! I was struggling and searching all the time for about 3 days!!!!!! Thank you really really much!!!! – user3734506 Jun 12 '14 at 17:37
0

You're badly confused. As one of the other posters pointed out, in your didSelectRowAtIndexPath method you're creating a new AufnahmeISTDetailTableViewController object, setting properties on it, then forgetting about it. Don't do that.

What you want to do is to set the value of an integer instance variable to the current row number, so you can remember it. Then, in your prepareForSegue method, get a pointer to the destination view controller, cast it to the correct type, and pass your saved integer value.

By the way, what is supposed to trigger your segue? Is selecting a row in the table view supposed to trigger a segue directly, or does the user have to do something else?

Duncan C
  • 128,072
  • 22
  • 173
  • 272