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.
Related
I have a requirement like this.
The app is implemented like a slide panel UI which we find in facebook or path app.
I am using JSSlidePanel (https://github.com/gotosleep/JASidePanels) and it requires 3 controllers to be added,
slidePanellcontroller.leftPanel = leftsideViewController;
slidePanellcontroller.centerPanel = centerViewController;
slidePanellcontroller.rightPanel = rightsideViewController;
I created a separate storyboards for slidePanellcontroller and for centerViewController. slidePanellcontroller is loaded from main storyboard, and centerViewController will be loaded from another storyboard called tabbar storyboard. Basically centerViewController is a tab bar controller.
Now I am not sure how to add the centerViewController's view into the slidePanellcontroller view.
Please provide any inputs.
Thanks,
Firsts instantiate the other storyboard:
UIStoryboard otherStoryboard = [UIStoryboard storyboardWithName:#"otherstoryboardfilename" bundle:nil];
Then instantiate the view controller by the identifier you set for it in Interface Builder.
UIViewController *centerViewController =
[otherStoryboard instantiateViewControllerWithIdentifier:#"centerViewControllerIdentifier"]
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.
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.
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.
I think I've found the cause: Document Info window in IB has a warning: "'Selected Navigation Controller (Second)' has nib name property set to 'SecondView.nib', but this view controller is not intended to have its view set in this manner."
Bummer.
I've built nib in Interface Builder that has UITabBarController at top level and switches between UINavigationControllers.
It works fine when everything is in a single nib file, but I'd like to use separate nib files for UINavigationControllers.
Starting with Apple's TabBar template, if I just change class of SecondView to UINavigationController, it all breaks:
and all I get is this:
// imgur has lost the image, sorry //
Is it possible to have separate file for UINavigationController without programmatically setting everything?
I would like TabBarController to handle loading and unloading of nibs.
Simply swap the UINavigationController with the FirstViewController.
So the hierarchy should be like this:
Tab bar controller
-----Tab bar
-----Navigation Controller
----------First View Controller
---------------Navigation Item
----------Tab bar item (First)
-----Navigation Controller
----------Second View Controller
---------------Navigation Item
----------Tab bar item (Second)
You set the nib of First View Controller in the inspector to the nib file containing the actual view objects (Since you are trying to split them into separate files, which is a good thing).
You have one tab, that tab has a navigation controller which loads First View Controller as its root view.
Done.
I haven't tried setting up UINavigationController via IB. I have multiple screens, each is stored in separate xib and there's a corresponding class that extends UIViewController. In applicationDidFinishLaunching I initialize UIViewControllers using xib's but then manually create UINavigationController, add navigation controller's view to window and push first view to navigation controller.
Not sure if that helps.
- (void)applicationDidFinishLaunching:(UIApplication *)application {
navigationController = [[UINavigationController alloc] init];
FirstViewController * viewController = [[FirstViewController alloc]
initWithNibName:#"FirstView"
bundle:nil];
[navigationController pushViewController:viewController animated:NO];
[viewController release];
[window addSubview:navigationController.view];
[window makeKeyAndVisible];
}
Above FirstViewController extends UIViewController, in IB you create your view then set File's owner class to your class (e.g. here FirstViewController) and connect the File's owner view to the UIView's view.
I believe you are looking for something like this. You would replace "whatever" with the name of you second nib file.
newNavController = [[UINavigationController alloc] initWithNibName:#"whatever" bundle:[NSBundle mainBundle]];
First, it looks like you have your UITabBarItems under the navigation controllers instead of directly under the UITabBarController. That may be part of your problem.
Second, when you add a UITabBarController in IB and and click on its icon in your list of top-level objects (your first screenshot), the attributes inspector will allow you to change the type of view controller for each of the tabs. Using this, you can change them all to navigation controllers, if you wish. Also, since you wanted to load custom views and view controllers from other nibs, if you look at the "View Controller" section at the bottom of the attributes inspector, you can select a nib from your project to load the view from. Assuming that nib's "File's Owner" is set to your UINavigationController subclass, it should all work fine.
All of this without a large amount of coding work, either. Let me know if you'd like screenshots for what I'm talking about in case you can't find these panels.
I found the same warning.I have kept all view controller in separate xib files. I got rid off it by removing .nib name and keeping it empty.