I have a navigation controller with a tableview inside on iphone version. Basically when I tap some row in this table A, sub-tableview controller A1 or A2 is pushed. Now on iPad, I need to show A1 or A2 in detailview in a splitview controller. How should I transform them? It's not in a root view controller.
I tried put my 'table A' view controller in master VC and a blank navigation controller in detail VC in the splitview, but how can I tell detail VC to show what after I select some row in table A?
Thanks...I tried but can't find any solution. I thought it would be pretty easy.
Have you looked at the template master-detail project that Xcode creates? It demonstrates the approach pretty clearly. Specifically have a look at the didSelectRowAtIndexPath: method of the masterViewController
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
NSManagedObject *object = [[self fetchedResultsController] objectAtIndexPath:indexPath];
self.detailViewController.detailItem = object;
}
}
Essentially your master view controller has a property that holds a reference to your detail view controller. This allows you to send a message to the detail view controller when it needs to display an object
Related
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.
In a split view controller app, how can I push multiple detail view controllers upon selecting a table row in the master view controller?
Just to be clear, I have splitviewcontroller with two different class:
1) MasterViewController - left handside View.
2) DetailViewController - right handside view
I need to add multiple ViewController above the DetailViewController using PushViewController (as a Stack), when I select a row in the master view controller. How do I wire up the view controllers? From the split view controller? or from the detail view navigation controller?
Assuming you want to replace the second view controller with another one, you can should be able to access the UISplitViewController via the parentViewController property of your master view controller and set a new viewControllers array. (See documentation at https://developer.apple.com/library/ios/documentation/uikit/reference/UISplitViewController_class/Reference/Reference.html)
- (void)tableView:(UITableView *)tv didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// ...
self.parentViewController.viewControllers = #[self, newViewController];
}
Assuming you are in your master view controller containing the table and it has a reference to the currently presented view controller which is a UINavigationController you can probably push the desired new view controller on the stack, no matter how often:
- (void)tableView:(UITableView *)tv didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// ...
[self.detailViewController pushViewController:vc1 animated:YES];
[self.detailViewController pushViewController:vc2 animated:YES];
[self.detailViewController pushViewController:vc3 animated:YES];
}
This will add three view controllers to the view stack of the detailViewController. Adjust the variable names to your situation of course.
In a split view controller app,how can I segue to different detail view controllers upon selecting a table row in the master view controller?
Just to be clear, I need the detail view controller to be replaced when I select a row in the master view controller. How do I wire up the view controllers? From the split view controller? or from the detail view navigation controller?
Implement tableView:didSelectRowAtIndexPath: in the master table view's delegate. Depending on the value of the indexPath parameter, call [detailViewController performSegueWithIdentifier:sender:] with the segue identifier of your choice.
In your tableView:didSelectRowAtIndexPath: method, do this:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self performSegueWithIdentifier:#"YourSegueIdentifier" sender:self];
}
If you need to perform different segues based on the selected row, do this:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *segueIdentifier = nil;
switch(indexPath.row) {
case 0:
segueIdentifier = #"YourSegueIdentifier";
break;
case 1:
segueIdentifier = #"ADifferentSegueIdentifier";
break;
.
.
.
}
if (segueIdentifier != nil) {
[self performSegueWithIdentifier:segueIdentifier sender:self];
}
}
// Get detail navigation controller
UINavigationController *detailNavigationController = [splitViewController.viewControllers objectAtIndex:1];
// Push the detail view controller
[detailNavigationController pushViewController:anyDetailViewController animated:NO];
// You also might need to set the splitview controller's delegate to this view controller
splitViewController.delegate = anyDetailViewController;
Use this code:
UINavigationController *detailNavigationController =[[[self splitViewController] viewControllers] objectAtIndex:1];
[detailNavigationController pushViewController:"your_view_controller" animated:YES];
In your segue, set your style to "Push", and your destination to "Detail". Current will push the destination view controller onto your Master view, whereas Detail will push it into the "Detail" view. It's that simple. Then wire it up the same way you wire everything else up.
But be careful, if you don't implement a way for it to wait for a previous segue, you can get an "Unbalanced calls" error if a new Controller is pushed onto the detail view before it's done dismissing/pushing another one. Double tapping a cell in a table will do it.
I have a simple test project. A UITableViewController (MasterViewController) embedded inside a navigation controller in storyboard. I am NOT segueing using the prepareForSegue to pass data to another view controller (DetailViewController). Instead, didSelectRowAtIndexPath is use to update a label in the detailviewcontroller as below:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
DetailViewController *detailViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"DetailViewController"];
NSMutableString *object = thisArray[indexPath.row];
detailViewController.passedData = object;
[self.navigationController pushViewController:detailViewController animated:YES];
}
Everything till this point works fine.
Now i have added another view controller in my storyboard. Made it the initial view controller, added two containers in it, then embedded both MasterViewController and DetailViewContainer in these containers.
Now instead of showing passed data inside the DetailViewController on the right side, its showing the passed data on the left side by replacing the controller view.
If i am not able to clarify what i am trying to say, here is the link to the project https://jumpshare.com/v/UiTFEB6AamIo8qX9sinW , its just for learning purpose.
Thanks
You're getting this problem because you are still doing this:
[self.navigationController pushViewController:detailViewController animated:YES];
The navigation controller you're referencing here is the one your master controller is embedded in, so you create an instance (different than the one that's already on screen) of detailController and push that onto the navigation controller.
What you want to do, is get a reference to the detail controller that's already on screen -- both child view controllers (the ones in the container views) are already instantiated when the app starts. So, you need to do this:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
DetailViewController *detailViewController = [self.navigationController.parentViewController childViewControllers][1];
NSMutableString *object = thisArray[indexPath.row];
detailViewController.passedData = object;
}
This will pass the value to the detail controller, but you can't have the code to update the label in viewDidLoad, since that view is already loaded, and won't be called again. Instead, override the setter for passedData, and update the label there (note that I changed the name of the argument to passedInData, so it doesn't conflict with your property passedData):
-(void)setPassedData:(NSString *)passedInData {
passedData = passedInData;
detailDescriptionLabel.text = passedData;
}
Bu the way, unless you're planning on adding other controllers after your master view controller, there's no reason to have it embedded in a navigation controller at all, given this set up. If you take it out, then you need to remove the reference to self.navigationController when you get the reference to the detail controller. It would then just be:
DetailViewController *detailViewController = [self.parentViewController childViewControllers][1];
Hi
anybody can explain of how to show a splitview when I tap a row from a cell??
or a tutorial of it??
I have a tableview, and i want to display information about that cell in a splitview.
Thanks
I don't think there's a tutorial for that, since you're not supposed to do that.
The split view controller is supposed to be the root view controller of your application, so if you want a pushable split view controller you'll have to write it yourself (or possibly find an open source one).
I want to implement an app that when it press a row from a table, it will open a splitviewcontroller that show the information
about the selected cell in the "detail´s splitview" , and all the info of the table in the "rootview".
I have copy to my project the
DetalviewController.h/m
RootViewController.h/m
DetailView.Xib
files from the splitview template.
And add this code to my tableviewcontroller:
(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//Initialize the controller.
if(split == nil)
split = [[RootViewController alloc] initWithNibName:#"split" bundle:[NSBundle mainBundle]];
//Pass the current row number to the sub view controller.
split.number = indexPath.row;
//here is my problem, i cant add the new view, and it crash
[self.view addSubview:[split view]];
}
enter code here