How to open view controller from didSelectRowAtIndexPath within storyboard?

妖精的绣舞 提交于 2019-12-09 12:46:28

问题


I have a storyboard with tabbarcontroller. One of tab bar has a tableview and I want that when the user tap in a row from tableview open a detail view. The problem is when I open detail view tab bar and navigation bar hides... In the storyboard I create the detail view as a new view controller, then I create a new file and referred it to the class of detail view .

The code in didselectrowatindexpath:

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

detalleYouTube *dvController = [[detalleYouTube alloc] init];

[self.navigationController pushViewController:dvController animated:YES];

}

Thank you in advance!


回答1:


I presume you have the 'Detail' view as part of the storyboard (not in a separate XIB), if so you will need to place a separate NavigationController at the start of the 'Detail' TabBarItem seque.

This page has a good tutorial on what I think your trying to achieve: http://maybelost.com/2011/10/tutorial-storyboard-in-xcode-4-2-with-navigation-controller-and-tabbar-controller-part1/

Also check these links to a more in-depth Storyboard tutorial:

http://www.raywenderlich.com/5138/beginning-storyboards-in-ios-5-part-1

http://www.raywenderlich.com/5191/beginning-storyboards-in-ios-5-part-2




回答2:


This is kinda old but if someone needs to do this here's an easy approach:

You can use add a segue from the view in the tab bar to detalleYouTube, put an identifier to the segue and do this:

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



回答3:


Another approach to this is not to use tableView:didSelectRowAtIndexPath but instead use prepareForSegue:sender

the way I did it was:

-(void)prepareForSegue:(UIStoryboardSegue*)segue sender:(id)sender
{
    DetailViewController *viewController = [segue destinationViewController];
    CustomObject *custObject = [arrayOfObjects objectAtIndex:[self.tableView indexPathForSelectedRow].row];
    viewController.objectNeeded = custObject;
}

This example is based on the idea that your detail view controller is connected to your table view controller.



来源:https://stackoverflow.com/questions/8724779/how-to-open-view-controller-from-didselectrowatindexpath-within-storyboard

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!