I have a UIViewController, and it has another UITableViewController as subview.
Table view is not visible, but in sometime it is visible by user's action. (for example, search table view.)
so, two new controller has no relation actually, (but UIViewController has UITableViewController as member variable), but only its view has parent - subview releation.
In this situation, user click the cell item in the table view, I have to push new view controller to navigation controller.
but only parent UIViewController is in navigationController, subview UITableViewController is not.
so code
[self pushViewController:viewController animated:YES];
fails in UITableViewController.
I solved this problem by passing navigationController instance of parent UIViewController to UITableViewController as property named parentNavigationController, and I called
[self.parentNavigationController pushViewController:viewController animated:YES];
instead.
It is solution that is not so bad, but I thinks it is some confused, I want to know is there more clearing solution.
How do you programming in this situation, friends?
I think parent-sub view relation of two view controller is confused, in the first. Is it better if I manage two views in one viewController? If then, view property of UITableViewController can be not table view? (and that view has table view as subview)
Thanks for your support, in advance.
Actually the concept is to push view controllers to the navigation stack of navigation controller. And UIViewControllers can only present modal view controllers, they can not push view controllers. So, sending pushViewController message to self(which is a UIViewController) is wrong. You can push a view controller using [self.navigationController pushViewController:anotherViewController];
In UITableViewController controller use self.navigationController instead of self.
[self.navigationController pushViewController:viewController animated:YES];
Related
I have UITabbarMoreController which contains a few UINavigationControllers. These UINavigationControllers then contain my custom view controllers.
So the hierarchy looks something like this:
UITabbarController
- UINavigationController
-> my custom UIViewController
- ...(other children of UITabbarController look the same)
From within my custom view controller I call [self parentViewController] which should return a UINavigationController. And this really happens, I really do get a UINavigationController, BUT only when the particular UINavigationController is NOT inside moreNavigationController.
Since I have many children controllers in the tabbar controller, the tabbar controller creates a moreNavigationController. If I open a viewcontroller that is under moreNavigationController and call [self parentViewController] it returns a mysterious object with class UIMoreNavigationController
I really need to get the UINavigationController that is parent of my view controller, not the UIMoreNavigationController. Also, I tried using [self navigationController] with the same result. How can I get reference to the real closest parent of my viewcontroller? Thanks for help in advance!
In short, you can't.
Apple always try to optimise their code. One of their optimisation here is to check whether the ViewController displayed in the list of its UIMoreNavigationController is of type UINavigationController. Because UIMoreNavigationController itself is a UINavigationController it does not want to create another UINavigationController. It's trying to avoid UINavigationController nesting.
When you look at the header file of UIMoreNavigationController you will notice it has a variable
UINavigationController* _originalNavigationController; which is accualy the original UINavigationController that you created and that you want to access. Unfortunetly you cant since UIMoreNavigationController is private.
Workaround (ugly)
Pass the reference of your NavigationController to its children when you push them on it's stack.
I have a root view controller that subclasses UINavigationController. It loads in a child view with a UIButton. When that button is pressed I want to make a call from the child view's corresponding view controller (lets say ChildViewController) to the UINavigationController's pushViewController: method in the parent controller.
How is this possible without directly referencing the parent view controller? Is it achievable using a standard protocol method or do I have to create my own?
Every UIViewController has a property called navigationController. If a UIViewController is a part of a UINavigationController's stack, you can use the navigationController property in the following manner:
[self.navigationController pushViewController:yourNextViewController animated:YES];
There's no need to access the rootViewController only for pushing a new ViewController on the stack. This could get really awful if you had big navigation stacks.
By the way - Apple states that UINavigationController is not intended for subclassing. Usually, it is a good idea to listen to their warnings and directions, so you may want to revisit the subclassing approach again.
using a subclassed UIViewController which is loaded to the UINavigationController's stack may prove a better approach.
Hope this helps.
The child UIViewController that contains your child UIView and UIButton should have the parentViewController property. You can use that to get a weak reference to your UINavigationController where you can message pushViewController:animated:
I have UINavigation Controller that has a UIViewController pushed onto the stack and displayed.
That UIViewController has a UITableView and uses an external class for that UITableView's datasource and delegate. I do this because I need to swap that datasource dynamically. Everything works beautifully for displaying data.
However, when handling didSelectRowAtIndexPath "down" in the delegate
I'm struggling with the proper way to call all the way back to the UINavigationController. Whereas I'm used to being able to do something like this when the UIViewController and Delegates are the same object:
ExampleViewController *newViewController = [[ExampleViewController alloc] init];
[self.navigationController pushViewController:newViewController animated:YES];
[newViewController release];
I cannot do so from the delegate and I'm just not getting what the correct reference back to the UINavigationController should be is the pushViewController.
Do I need to set an ID similar to a delegate so that the UITableViewDelegate has a pointer directly back to the UINavigationController?
Only UIViewController instances that have been pushed onto a navigation stack have convenient access to a corresponding UINavigationControllerĀ instance through the "navigationController" property. So you have a couple of options:
1) Pass a reference to the navigation controller to your table view delegate(s) so they can call the "push" method you're used to using.
2) Pass a reference to your view controller to your table view delegate(s), and have them call a method when a user taps a table view row. You can write your own delegate protocol and implement it in your view controller to formalize this approach.
Maybe this is a too simple question but I'm kind of stuck here. I've implemented a class that inherits from UITableViewController. This class is the root controller of a split view I'm building by code, not with Interface Builder. The problem is that I'm trying to show a detail view from the accesory view in the table, and the navigationController attribute in my instance is nil. I don't have any idea of how to instantiate a new UINavigationController to be able to display a detailed view in my code.
This is how I'm trying to use the accesory button:
- (void)tableView:(UITableView *)tableView
accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath {
PartDetailViewController *partDetailViewController =
[[PartDetailViewController alloc] initWithNibName:#"PartDetailView" bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:partDetailViewController animated:YES];
[partDetailViewController release];
}
Any hint would be really appreciated.
Thanks in advance,
Federico
If self.navigationController is nil means that you have not "pushed" your UITableViewController instance inside a navigation controller.
So I can imagine, from your description, that you have an iPad app with a UISplitViewController and your table has been instantiated inside the "root" view controller of the split view, so having a hierarchy like:
UISplitViewController ==>(root)==> UITableViewController
. If this is the case, what you have to do is to create a UINavigationController, push the table view controller inside the UINavigationController and then define the split view "root" controller to be the UINavigationController, according to this schema:
UISplitViewController ==>(root)==> UINavigationViewController ==> UITableViewController
Hope this can help you.
Create a UINavigationController, add the UITableViewCOntroller to the UINavigationController and add the UINavigationController to the split view's view.
my app first viewController is UIViewController.
and when user click button firstView disappear and push UITabViewController
is it possible?
i can't find how to push UITabViewController from UIViewController.
UPDATE sorry, I misread TabVC for UITableViewController. Do you mean UITableViewController or UITabBarController? I'll leave my answer below anyways.
In this instance, it's usually best to have a UITabBarController be the root view object. Although it can be done, it's a messier implementation, in my opinion.
I would in fact make the UITabBarController the root and display the UIViewController modally from that UITabBarController on launch.
The user would be presented with the UIViewController and when they clicked the button, dismiss that modal view, revealing the UITabBarController.
Just use a UINavigationController.
Use the navigation controller to push the tableView as the second level in the hierarchy. As a bonus you'll get the back button for 'free' and you don't have to worry about delegates for getting back to the original UIViewController.
you may try this:
self.tabBarController.selectedViewController
= [self.tabBarController.viewControllers objectAtIndex:2];
it should work because selectedViewController property contains view of selected tab.
First of all you have view controller . And make Second view controller which contain tabbarcontroller . Now just push second view controller . And add tabbarcontroller's view as a subview to second view controller .
Hope you gets it ..