I have a viewcontroller with multiple tableviewcells in it. I want move from one of the tableviewcell to another viewcontroller using storyboard. I have a navigation controller for viewcontroller. I didn't really find a way to solve this.
You should implement didSelectRowAtIndexPath delegate, then some logic for move to another view controller.
#interface ViewController()<UITableViewDelegate>
...
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
AnotherViewControllerClass *vc = [[UIStoryboard storyboardWithName:#"StoryboardNameOfAnotherViewController" bundle:nil] instantiateViewControllerWithIdentifier:#"AnotherViewControllerIdentifier"];
[self.navigationController pushViewController:vc animated:YES];
}
if you want to do with storyBoard then you can use Segue to jump from one tableview cell to another ViewController what you have to do is
1). right click and drag from your table view cell to your desired view Controller and then release it you would see a pop up select "show"
2). click the segue and go to identity inspector and name the segue identifier as you wish
3).then you have to just write a method prepareFor segue methodand in that write if statement like this
if (segue.identifier == "your identifier Name")
{
//`enter code here`write your logic of pushing the viewcontroller
}
the above code for if is in swift and you can repeat the steps for your all multiple tableviewcell with diffrent identifier name
if not using storyboard then use this method
if (indexpath.row == 0)
{
//use the push viewcontroller code here
}
for each index path you can check and push diffrent viewcontrollers
Try this..
Open Storyboard and select your initial view controller
Select editor from top bar menu in your Xcode
Select Embed in and choose UINavigationController
And run your code again...
Related
I created the single view application using storyboard.
In storyboard file, i have three view controller
1-navigation controller 2-Recipe Book View Controller 3-View
Controller
Prototype cell of the table view of Recipe Book View Controller is connected through push segue to View Controller.Is the problem Recipe Book View Controller does not navigate to View Controller?
here is the sample of project for download.
https://drive.google.com/open?id=0B5pNDpbvZ8SnLTd1R3NBTE1ReEk
I just Check your project.
Do following steps:-
In Main.storyboard, click on Push segue to ViewController and add name of identifier
Go to ViewController.m and paste below code
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[self performSegueWithIdentifier:#"yourSegueName" sender:self];
}
Following mistakes are found in your project.
There is no class for Third VC.
PrepareForSegue not implemented.
You wanted to push VC on TableViewCell selection but there you does not implemented didSelect tableview delegate.
Select the segue connecting RecipeBookViewController to ViewController , Then give that segue an identifier, i.e. "ViewControllerSegue".
Implement the delegate method of table view and call the performSegueWithIdentifier method :
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[self performSegueWithIdentifier:#"ViewControllerSegue" sender:tableView];
}
Based on your Source code you have to add some code & classes in your project to achieve your requirement.
1st You have to add RecipeViewController to control your Recipe List and Tableview both.
2nd as You have created this RecipeViewController you need to assign this class to respective ViewController in your storyboard.
3rd Assign Segue from PrototypeCell to ViewController where you need to push.
Hope you can understand whats wrong in your Code.
I have a custom tableview cell in xib file. I am using this custom cell in my FirstViewController. I want to show SecondViewController on didSelectRowAtIndexPath of first viewController.
And I am performing segue in didSelectRowAtIndexPath like
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self performSegueWithIdentifier:#"secondViewControllerSegue" sender:indexPath];
}
I am getting error saying -
'Receiver () has no segue with identifier 'secondViewControllerSegue''
What am I missing here?
Go to your storyboard and Ctrl+Drag from the first view controller (the yellow icon of the first view controller) to the second to create a segue. Then on the left panel go to attributes inspector tab and set an identifier for your segue. Then just set the same identifier in your performSegue method.
You have to connect a segue from firstViewcontroller to secondviewcontroller with the name you are using for segue.
And stroyboard id is not for segue it is for identifying controller in the the storyboard.
You have to create segue between you first and second view controller the rest of you code is fine.
I have storyboard named "MainStoryboard_iPhone.storyboard" and it has 2 views. The first view has a table cell with disclosure button. I can drag drop the table cell to other view and show the view, but I need to do it programmatically only when user click on accessory button without using segue. The name of 2 views are RootViewController and ProviderLookupViewController. Currently I have tried this code but doesnt work, it throws error when I click on the accessory button on the cell. Commented lines are the other things that I tried
-(void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{
UIStoryboard* sb = [UIStoryboard storyboardWithName:#"MainStoryboard_iPhone"
bundle:nil];
// ProviderLookupViewController* vc = [sb instantiateViewControllerWithIdentifier:#"ProviderLookupViewController"];
UIViewController* vc = [sb instantiateViewControllerWithIdentifier:#"ProviderLookupViewController"];
[self presentModalViewController:vc animated:YES];
//[self.navigationController pushViewController:vc animated:YES];
}
If I am correct:
1) From your question it seems that you have a story board with two view controller, the first one is a UITableViewController and the other a subclass of UIViewController.
2) You want to present the second view when the user clicks the disclosure button.
Given that you use a storyboard instead of nib files, I recommend that you use a segue, it will solve you problem without writing code.
A table view cell can have two segues, one if you select the cell and another if you select the accesory button. Control drag from from the accessory button to the second view controller instead of doing the control drag from the cell and when the menu appears you have to select an option below Accesory action instead of below Selection Segue
The prototype cell of the table view has to have the disclosure accessory enable. Do it from the utilities pane (the right one). Accessory can't be none do this. In the picture accessoy is Detail disclosure.
The utilities pane also let's you do the segue. Use accessory action, though, teh picture is from one of my projects.
First give storyboard id as ProviderLookupViewController in your scenario and tick use storyboard Id then
ProviderLookupViewController* vc = [self.storyboard instantiateViewControllerWithIdentifier:#"ProviderLookupViewController"];
[self presentModalViewController:vc animated:YES];
Note:ProviderLookupViewController is your identifier.
Create a segue in IB from one view controller to another. Add some identificator to segue and just use
[self performSegueWithIdentifier:#"SomeSegueName" sender:someObject];
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
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"; }