Can I change View to the ViewController when click the TableViewCell? - ios

I am new to Objective-C.
It has TableViewCell and the TableView on the ViewController like the following picture.
I want to change the View to another Viewcontroller when I click the TableViewCell.
But it seems can not use the performSegueWithIdentifier to change the View in didSelectRowAtIndexPath without using UINavigationController , The error log like the following:
Push segues can only be used when the source controller is managed by an instance of UINavigationController.'
Is there has another way to change the View to another Viewcontroller when I click on the TableViewCell ?
Thanks in advance.

In Interface Builder(storyboard) click the following:
Exactly what the debugger says: Push segues can only be used when the source controller is managed by an instance of UINavigationController.' Then you'll be able to push segues.

U can present view controller using this
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
yourViewController *vc = [[yourViewController alloc]initWithNibName:#"yourViewController" bundle:[NSBundle mainBundle] ];
[self presentViewController:vc animated:YES completion:nil];
}

Related

pushing a DetailViewController from a table in another DetailViewController programatically

I used ShowDetailSegue to push a DetailVewController from another DetailViewController. there is no issues. But my need is to push to a DetailedViewController from a TableView in another DetailedViewController.
I used the below code for pushing:-
PushUpExerciseViewController *pushUpObj = [self.storyboard instantiateViewControllerWithIdentifier:#"PushUpID"];
[self presentViewController:pushUpObj animated:YES completion:nil];
After using this code in my TableView DidSelectRowAtIndexPath the MasterViewController is missing. i need both Master and Detailed ViewControllers. How to push from TableView in DetailViewController to another DetailViewController Programatically
Simply push to View Controller.
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
DetailVewController *objDetailVC = [self.storyboard instantiateViewControllerWithIdentifier:#"DetailVewController"];
//Pass the data here to DetailViewController
[self.navigationController pushViewController:objDetailVC animated:YES];
}
As you told you need to push DetailedViewController from another DetailedViewController instance on TableViewCell selection. In this case you need prevent the infinite loop of push.
Flow
Master (Push) -> Detail (Push on didSelectRowAtIndexPath ) -> Detail

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

Storyboard Segue From View Controller to Itself

I am trying to make a mechanism to drill down a file / folder list. The idea is to show the same file list view controller every time the user selects a folder, and show a file detail view controller if he/she selects a file.
So far, I have created a segue from the file list view controller to the file detail view controller, and a segue from the file list table view cell to the the file list table view controller:
The issue with this is that as soon as the user taps the cell, the segue is executed. I would like to remove the segue from the table view cell and make one from the file list view controller to itself. That way, I could trigger the right segue programmatically when the user tapped the cell.
So, my question is: Is it possible to create a segue from a view controller to itself in Interface Builder?
If you are using a navigation controller you need to push the ViewController into the nav stack. In this example, i named my ViewController "VDI" in my Storyboard ID setting.
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle: nil];
YourVC *dest = [storyboard instantiateViewControllerWithIdentifier:#"VDI"];
[self.navigationController pushViewController:dest animated:YES];
If you don't want the NavigationController to keep adding itself into your "Back" history you can pop the stack before adding to it like so.
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle: nil];
YourVC *dest = [storyboard instantiateViewControllerWithIdentifier:#"VDI"];
UINavigationController *navController = self.navigationController;
[navController popViewControllerAnimated:NO];
[navController pushViewController:dest animated:YES];
Using Xcode 5 there is a much simpler solution.
Click the table cell in the storyboard
Open the Connections Inspector (right arrow icon in the upper right)
Under "triggered segues" you see "selection"
Drag from the circle next to "selection" to the cell in the storyboard
That's it.
I developed a method to create a segue using a phantom button. I believe it will solve your problem. You can read about it in my answer here.
Instead of performing a segue to the same controller, you can instantiate a view controller (the same one) from storyboard, and then push that onto the navigation controller.
Interface Builder approach: Just segue to a storyboard reference which refers back to the presenting view controller.
The correct answer is to use a Storyboard Reference that is referencing the UIViewController you want to segue to itself and then point the segue at it.
In IOS 6, there is a cleaner solution than using a phantom button. You can still define the segue from the table cell to the view controller, and look at the sender to cancel the automatically triggered segue:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//storyboards should use segues and override prepareForSegue instead
//but here we need custom logic to determine which segue to use
id item = [self.fetchedResultsController objectAtIndexPath:indexPath];
if (item meets condition) {
[self performSegueWithIdentifier:#"segue1" sender:self];
} else {
[self performSegueWithIdentifier:#"segue2" sender:self];
}
}
- (BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender {
//ignore segue from cell since we we are calling manually in didSelectRowAtIndexPath
return (sender == self);
}
Here's how you can push another instance of the current view controller without defining a segue or hardcoding its own identifier:
SameViewController *same = [self.storyboard instantiateViewControllerWithIdentifier: self.restorationIdentifier];
[self.navigationController pushViewController: same animated: YES];
You just need to set the Restoration ID to be the same as Storyboard ID (there's a checkbox for that in IB).
Hope this helps.
I found that you can create multiple prototype cells.
Than you can link every cell (in the Storyboard) to a different View.
Something like this:
NSString *CellIdentifier = #"Cell";
if (Condition2 ){
CellIdentifier = #"Cell2"; }
if (Condition3 ){
CellIdentifier = #"Cell3"; }

How to open view controller from didSelectRowAtIndexPath within storyboard?

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

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