How to open view controller from didSelectRowAtIndexPath within storyboard? - uitableview

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!

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];
}

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.

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

Related

In UISplitViewController, can't make showDetailViewController:sender: push onto detail navigationController

In iOS 8, view controllers can now call showDetailViewController:sender: to have the system determine the proper view controller to present the detail view controller.
In my app, I have a UISplitViewController, which contains two UINavigationControllers in its viewControllers array. The first UINavigationController contains my 'master' view, a subclass of UITableViewController. The second UINavigationController contains my 'detail' view.
Since I'm trying to make this work universally, I'm trying to use showDetailViewController:sender: to display the detail view:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
self.itemVC.item = self.itemStore.items[indexPath.row];
[self showDetailViewController:self.itemVC sender:self];
}
This works fine with the Horizontal Compact trait (iPhone style), when self.splitViewController.collapsed == YES, but not when the trait is Regular (iPad, not collapsed). On the iPad, it replaces the detail UINavigationController with the bare detail view controller (instead of replacing that UINavigationController's viewControllers array).
To get around this, I'm tested for whether or not it's collapsed, and if it isn't, I'm wrapping the detail view controller in another UINavigationController before showing it:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
self.itemVC.item = self.itemStore.items[indexPath.row];
UIViewController *vcToShow;
// For whatever reason, when not collapsed, showDetailViewController replaces the detail view, doesn't push onto it.
if (self.splitViewController.collapsed) {
vcToShow = self.itemVC;
} else {
vcToShow = [[UINavigationController alloc] initWithRootViewController:self.itemVC];
}
[self showDetailViewController:vcToShow sender:self];
}
I suppose alternatively I could just configure self.itemVC and avoid calling showDetailViewController:sender: altogether when self.splitViewController.collapsed == NO:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
self.itemVC.item = self.itemStore.items[indexPath.row];
// For whatever reason, when not collapsed, showDetailViewController replaces the detail view, doesn't push onto it.
if (self.splitViewController.collapsed) {
[self showDetailViewController:vcToShow sender:self];
}
}
But, this feels like it's defeating the purpose of showDetailViewController:sender:, which is to loosen up the coupling between self and the rest of the view hierarchy.
Is there a better way to handle this?
In showDetailViewController:sender: depending on the collapse property you need to create the controller you want to show in the detail.
E.g. On the iPad in landscape mode it would already create the detail view controller from the storyboard but on the iPhone 5 where it is collapsed the view controller does not exist yet.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UINavigationController *detail;
ImageViewController *imageVC;
// on the iPhone (compact) the split view controller is collapsed
// therefore we need to create the navigation controller and its image view controllerfirst
if (self.splitViewController.collapsed) {
detail = [[UINavigationController alloc] init];
imageVC = [self.storyboard instantiateViewControllerWithIdentifier:#"ImageViewController"];
[detail setViewControllers:#[imageVC] animated: NO];
}
// if the split view controller shows the detail view already there is no need to create the controllers
else {
id vc = self.splitViewController.viewControllers[1];
if ([vc isKindOfClass:[UINavigationController class]]) {
detail = (UINavigationController *)vc;
imageVC = [detail.viewControllers firstObject];
}
}
[self prepareImageViewController:imageVC forPhoto:self.photos[indexPath.row]];
// ask the split view controller to show the detail view
// the controller knows on iPhone and iPad how to show the detail
[self.splitViewController showDetailViewController:detail sender:self];
}
I hope this solves your issue.
The way You doing it have a problem. If your rotate the device(change the mode from collapsed to allVisible) after you select, you will find the detail vc without a navigation controller.
If you call showDetailViewController:sender: in all cases and pass the view controller with a navigation controller it will work fine in both cases and also will fix the rotaion problem mentioned above.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
self.itemVC.item = self.itemStore.items[indexPath.row];
UIViewController *vcToShow= [[UINavigationController alloc] initWithRootViewController:self.itemVC];
[self showDetailViewController:vcToShow sender:self];
}
if (self.splitViewController.collapsed)
[self.splitViewController showDetailViewController:self.itemVC sender:self];
else
self.splitViewController.preferredDisplayMode = UISplitViewControllerDisplayModePrimaryHidden;

cannot transition between view controller when clicking a table cell

I want to transition from one view controller into another when clicking a table cell without any additional data passing. Here is a screenshot of my storyboard: http://imgur.com/C8yTRaZ
However, it doesn't go to the second view controller when I am clicking on the table cell. They are both embedded in the navigation controller (I think; since the "Navigation Item" is in both controllers). What am I missing?
Make sure you have mapped your table data source and table delegate to self.
For that you could use didSelectRowAtIndexPath in that you could programmatically call that other view Controller.
Like below:-
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
ViewController *viewController = [[ViewController alloc] init];
[self presentViewController:viewController animated:YES completion:nil];
}
This will help you out.
Make sure that 'user interaction' for both 'table view' and 'table view cell' should be enabled.

Push a new view controller with navigation controller

So I created a new view controller which has the Custom class named DescriptionViewController.
And from my current view controller inside didSelectRowAtIndexPath I use this code:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
DescriptionViewController *descriptionViewController = [[DescriptionViewController alloc] initWithNibName:#"DescriptionViewController" bundle:nil];
[self.navigationController pushViewController:descriptionViewController animated:YES];
}
But nothing happens when I click on a cell. Is there anything more I need to do ?
If you put in a breakpoint and it is stopping in the didSelectRowAtIndexPath, my guess is that your self.navigationController is nil.
If you are using storyboards, make sure your entry point is a UINavigationController and not a UIViewController.
If you loading this view controller though a modal transition, make sure you push on a navigation controller with this view controller as the root view instead.
Select your viewcontroller in the storyboard and on top go to editor->embed in->navigation controller then instead of initWithNibName try just init

what is the correct approach to push or segue a new view from only one cell of a dynamic tableview

IOS 6 , XCode 4.6.2 using storyboards
Heres my problem. I have a tableview with 7 cells using dynamic prototype. I would like to segue from the third cell to another tableview (which is going to allow the user to select a country and pass that country back.)
Is it possible to set this up so only that one cell triggers the segue ?
If its not, then I presume I would need to use the didSelectRowAtIndexPAth method - but if i haven't drawn a seque in the storyboard i cant call performSegueWithIdentifier because there is no identifier- and no segue
Any ideas what should do ?
Cheers
Simon
Don't draw your segue from the cell prototype in the table. Instead draw it from the Files Owner icon below your table view controller to the next table view controller. Give it an identifier and then call
[self performSegueWithIdentifier:#"yourSegueIdentifier" sender:self];
in tableView:didSelectRowAtIndexPath.
If you don't have a segue between view controllers you can use push methods of UINavigationController.
If you already have a segue between the two view controllers you can do this:
-(void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if(indexPath.row == yourCellIndex) {
[self performSegueWithIdentifier:#"yourSegueIdentifier" sender:objectThatYouSendOrNill];
}
}
If you are using static cells in your table view controller you can directly link segue from your cell to the destination view controller.
You can design the DetailViewController in storyboard itself. Give a StoryboardID for that view controller. and in didSelectRowAtIndexPath
-(void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.row == 3)
{
DetailViewController *viewController=[self.storyboard instantiateViewControllerWithIdentifier:#"DetailViewController"];
[self.navigationController pushViewController:viewController animated:YES];
}
}
Dont forget to give StoryboardId for the DetailViewController as DetailViewController

Problem In UISplitViewController in iPad

I am working on UISplitView Controller. I have used a SplitView controller template of Xcode. Actually i wanted to implement a scenraio in which when i click on table view cell it should add a New Root Controller with a table View into the Navigation Stack.
i did it using the following code
- (void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NextViewController *nextView = [[NextViewController alloc] initWithStyle:UITableViewStylePlain];
[self.navigationController pushViewController:nextView animated:YES];
[nextView release];
}
It works perfect and i have used the RootView Controller code into the NextViewController so that when we click on New Screen's cell it should update my Detail Screen. But unfortuantely it is not updating my Detail Screen.
What can be the reason ??
Please help friends..
Thanks
To updating your Detail Screen. you need to implement a delegate method of communication between Current RootView controller and Detailview controller.
Best Wishes,

Resources