简体   繁体   中英

navigation from tableview to another view

I am working in a table view app using story board . I want to perform event on selection

of cell in table view . So when I select a cell element , a next view(detailview) should be

open for displaying further details .

 -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
 {  
    self.dv = [[Detail alloc]initWithNibName:@"Detail" bundle:nil];


    dv.dic = [self.arr1 objectAtIndex:indexPath.row];


    [self.view addSubview:dv.view];
}

Instead try this:

 -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{  
       self.dv = [[Detail alloc]initWithNibName:@"Detail" bundle:nil];


        dv.dic = [self.arr1 objectAtIndex:indexPath.row];


       [self presentViewController:dv animated:YES completion:nil];

}

And if you want to dismiss use this code:

[self dismissViewControllerAnimated:YES completion:nil];

You should use a navigation controller and when selecting a row, push the detail view into it.

To create navigation controller: selected the tableview controller and go Editor / Embed in / Navigation Controller.

Then do:

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

{
    // dv = [Detail alloc] initWithNibName:@"Detail" bundle:nil];
    dv = [storyboard instantiateViewControllerWithIdentifier:@"DetailViewController"];
    dv.dic = [self.arr1 objectAtIndex:indexPath.row];
    [self.navigationController pushViewController:dv];
}

The commented line is if you're not using storyboard.

Or you can do as Abdullah suggested, but implement a dismiss button with

[self dismissViewControllerAnimated:YES completion:nil];

In the storyboard drag a segue from the viewcontroller icon at the bottom of your view to the next viewcontroller you want to segue to. Make sure that your view is embedded in a navigation controller. Press the arrow that appears between your tableviewcontroller and the target view controller and make sure you label it with a segue identifier. Then in the method you are describing make sure they selected the correct index path with an if statement and write

[self performSegueWithIdentifier:@"theNameOfYourIdentifierFromTheStoryboard"];

That being said, I think that there is a large body of information you should research.

Read this: http://www.appcoda.com/use-storyboards-to-build-navigation-controller-and-table-view/ as a start.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM