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.
Related
I have a UITableViewController and want to push a DetailViewController : UIViewController. Do I need to make the UITableViewController be part of a UINavigationController or can I just do a pushViewController from a UITableViewController?
I understand that UINavigationController (and UITabBarController) have a viewController's array that manages the viewing of view controllers. Does a UITableViewController have the same?
Sorry if question sounds convoluted.
thx
If you want a navigation stack where you can push and pop view controllers, you want the UINavigationController.
The easiest way to do it would be to place your UITableViewController into the UINavigationController (assuming that it is the first screen displayed) and then you can do a simple push from the controller. It would look something like this:
// First create viewController and then push it like this:
[self.navigationController pushViewController:viewController animated:YES];
I've created a storyboard that I want to display using my UINavigationController. The initial view in the storyboard is a UITableViewController so I have created a subclass of UITableViewController and set the class on the storyboard to match it.
I then try to display the storyboard like so:
StoryBoardView *newView = [[StoryBoardView alloc] init];
[self.navigationController pushViewController:newView animated:YES];
However my view is not shown for some reason. Please can someone help me?
When you push the view controller, you need to instantiate the view controller from within your storyboard, not just alloc/init a new instance.
StoryBoardView *newView = [[self storyboard] instantiateViewControllerWithIdentifier:#"<ViewControllerIdentifier>"];
Have you tried when viewing the storyboard, select your very first controller and tick 'Is Initial View Controller'?
That should start your app with that controller.
I have a custom UIViewController in Interface Builder (in my storyboard) and I want to add this view I assembled into another view programatically. I have made a class for the view controller I made but simply importing and adding that custom class as a subview doesn't appear to work.
Any help much appreciated, as always.
You'll need to load an instance of the view controller from the UIStoryboard object and add it's view as a subview. That code would look something like this:
UIStoryboard* storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
MyViewController* myVc = [storyboard instantiateViewControllerWithIdentifier:#"ident"];
[self.view addSubview:myVc.view];
Make sure you set the identifier field for your view controller in IB and pass that to the "instantiateViewControllerWithIdentifier" method.
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];
Is there anything special I need to do when adding a UIViewController into a nib? My -viewDidLoad method is not being called, even though the nib is being loaded and its subclass is set in IB to my view controller class.
http://dl.dropbox.com/u/448021/Test.zip
There's my test case. I just can't figure out why FooViewController -viewDidLoad isn't being called.
Thanks for the help.
The FooViewController you created there serves no purpose, if I see things correctly. In MainWindow.xib, you have a navigation controller and your own RootViewController. So far so good. You define the view of that in RootViewController.xib. Also ok. But the View Controller inside that last xib will do nothing, until you do something like
[self.navigationController pushViewController:detailViewController animated:YES];
(which is in your didSelectRowAtIndexPath)
The commented out part in didSelectRowAtIndexPath basically invokes a new viewcontroller when a user selects a row, and does so while loading the associated xib file, which is loaded in this line:
DetailViewController *detailViewController = [[DetailViewController alloc]
initWithNibName:#"Nib name" bundle:nil];
You could also create the viewcontroller in a nib file, like you have now, but then you would need to define an
IBOutlet FooViewController *fooVC;
and link that up within IB, and then push this fooVC onto the view stack when the user selects something - in that case you would skip the alloc / init line above.
Add a view to FooViewController.
Just go to interface builder and drag a view to FooViewController.