Cornelia G
Cornelia G

Reputation: 163

Navigate to the right view from cell

I have this code to navigate from my tableview to another tableview. But it doesnt navigate to the right tableview. It seems that it creates another view that it navigates to and not the one on my storyboard that I want to go to. Is this because I create an object?

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

    ValjKupongViewController *vk = [[ValjKupongViewController alloc]init];
    [self.navigationController pushViewController:vk  animated:YES];
}

How can I write this the right way so that I navigate to my view in storyboard that have the class ValjKupongViewController?

Very grateful for help!

Upvotes: 0

Views: 52

Answers (1)

Ríomhaire
Ríomhaire

Reputation: 3114

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *) indexPath
{
    [self performSegueWithIdentifier: @"My Custom Segue" sender: self];
}

In your StoryBoard

Step 1 Ctrl and Drag the mouse from the View Controller on the left to the View Controller on the right.

View Controller on the left = The View Controller you are transitioning FROM.

View Controller on the right = The View Controller you are transitioning TO.

Step 2 Select push as the type of Segue

enter image description here

Step 3 Now your View Controllers will have the Segue between them (as seen below)

enter image description here

Step 4 You need to name the Segue to be able to identify it.Click on the Segue, then go to the attributes inspector and set the Identifier. (In the example it is set to My Custom Segue. Make sure this is the same as in the code above.

i.e. [self performSegueWithIdentifier: @"My Custom Segue" sender: self];

enter image description here

Is this because I create an object?

Yes you are allocating and initialising a ValjKupongViewController object each time didSelectRowAtIndexPath is called. With this line of code

ValjKupongViewController *vk = [[ValjKupongViewController alloc]init];

You then transition to it with this line [self.navigationController pushViewController:vk animated:YES];

Upvotes: 2

Related Questions