Adding view to storyboard programmatically? - iphone

I am loading a viewController via a storyboard segue and I want to add a UIImageView to the controller programmatically (instead of creating it in the storyboard). Usually when working without a Storyboard or NIB I would do this in loadView: is there an appropriate place for this when using storyboards or does the storyboard drop you in a bit late in the day for that?
EDIT: Updated error regarding NIB and loadView:

If you're using a NIB you shouldn't really be adding anything using loadView:. You should be using viewDidLoad:. This also works when using Storyboards.

Related

Use storyboards in a project which has .xib files- iPhone

I want to use generally the old .xib files in my iPhone application. But when it comes to tableViewController storyboard is a lot more convenient in order to make custom cells etc. Is it possible to make a .xib based application and in the middle of it, to use a storyboard for a UITableViewController and its DetailedViewController only?
You can use a storyboard for any part of a program. Storyboards are not an all or nothing concept. You can have just one view controller in a storyboard, or a small network that just represents a subsection of your app.
To use just one view controller from a storyboard:
Add a new storyboard to your existing project.
Add a single view controller to the storyboard.
Assign an identifier to the view controller in the inspector.
Use the UIStoryboard class to load the storyboard resource
Use -[UIStoryboard instantiateViewControllerWithIdentifier:] to create a new instance of that view controller.
Install the view controller some place in your app by doing something like pushing it on to a navigation controller, or run it modally.
Both can work fine together (Storyboards and Nib files). In the TVC that is part of your storyboard, just instantiate the destination VC in code and use the usual initWithNibName method to load the nib file.
You can add a storyboard to any project, but the point of storyboards is to centralize your XIB files into one location rather than having 10 XIB files you can have 1 .storyboard file that contains 10 scenes representing your views. This shows your connections to other scenes, and you can manage all the seques and transitions of each scene. So is it possible, yes you could add a storyboard to your project, but I would recommend you design you entire application in a storyboard if you want to use them.

iPhone IOS5 Storyboard, how to load a UIViewController with a custom .xib file?

I have some UIViewControllers from my old iOS4 project, they are using .xib, created in interface builder.
My new project, built for iOS5, uses storyboards.
I'm trying to add a UIViewController to the storyboard, but have it use a custom XIB that I already have. I've set the controller's identity in the indentity inspector (in interface builder), but am not sure how to ask that controller to load a custom .xib.
Any help is appreciated!
PS. Up to date I was able to get around this by creating a "wrapper" class for the storyboard purposes, and have that class have another UIViewController. But this kinda defeats the whole point of a storyboard.
You could copy the contents of the xib into the storyboard and then instantiate it using:
- (id)instantiateViewControllerWithIdentifier:(NSString *)identifier
after setting the identifier on it. If you need the instance of the storyboard you can get it this way:
[UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
Mike K's answer is false.
While mixing XIBs and Storyboards doesn't really solve Alex's problem, it is quite possible to do within the same project.
See: https://stackoverflow.com/a/8516479/1309238 re: loading a second storyboard.
I'm not sure if including a "Main Storyboard" in your Target Summary is advisable if you plan to do this, but if you've loaded a XIB instead of a storyboard to begin with, you can modify the code linked above to load the storyboard after the XIB.
I'm not sure if you can jump into the middle of a storyboard from a XIB, which may have been what Mike meant.
You cannot mix xibs and storyboards in your project -- it's one of the other, I'm afraid.

Problems converting UITableViewController to plain UIViewController

I"m having some issues trying to manually convert a UITableViewController to a plain UIViewController. I need to change some of the layout of the view, so I'm just planning on creating a UITableView manually and positioning it on the screen myself.
I deleted the automated xib file that was generated by default from the project creation process. I changed the view controller to subclass UIViewController instead of UITableViewController as well and have it set adhere to the UITableViewDataSource and UITableViewDelegate protocols.
I also set a UITableview *tableView property & synthesizer, but I did not manually alloc or addSubview with it.
However, when I launch the app and the view loads, the tableView still shows on the screen. I just don't understand how this is happening.
There has to be some kind of vestigal code that was created automatically that I'm not finding and need to clear out, but I've looked through all the related files (app delegate, view controllers, xibs, etc...) and I can't seem to find anything.
You need to make some changes in nib file also. Try changing nib files connection.

Unable to change style of UITableView to Grouped in Interface Builder

It'a s simple app, window based.
The window has a UINavigation Controller, and the Controller has a UITableView built from
New File-UIViewController subclass- UITableViewController option check.
I set the style for the UITableView to Grouped in the Interface Builder, but the table stayed Plain in the Simulator.
I wonder if I missed any options?
XCode 3.2.6
iOS SDK 4.3
Any help is appreci
Project download: Zip File#Google Docs 692k
tViewController is a subclass of UITableViewController so it's creating a tableView for you and ignoring yours. Change the superclass of tViewController to UIViewController and it will use the tableView you have defined in IB.
Make sure you've properly specified the nib file (remember: iOS is case sensitive) for the controller and connected the table in the nib to File's Owner. Otherwise, the controller will create its own table, which is probably what's happening.

How to use UISearchDisplayController from a controller within an UITabBar controller?

I have an UITabBar controller managing several controllers (using SDK 3.0). One of these is a tableView controller and I need to provide a search capability using the UISearchDisplayController. All of my code is based on Apple TableSearch example. However, when clicking on the tab, the tableView controller appears showing its related content, but no searchBar appears. I have checked the xib in IB to make sure that all of the outlets are properly set, but no matter what I try self.searchDisplayController is always nil and the search bar does not appear.
In practice I have replicated MainView.xib from the TableSearch example and set the file's owner class to the correct controller class for the tab. The outlets are sets as in the example MainView.xib. Am i missing any important step or doing something wrong?
Thank you in advance.
I had the same issue and happened to stumble upon this solution ...
If you have your table view controller (eg. UISearchDisplayController) nested in a Tab Bar or Navigation controller using Interface Builder, you need to set the "Nib Name" in the "Attributes Inspector" window.
The Nib name will be the one that holds the table view and has the controller (eg. UISearchDisplayController) as the File's Owner.
I too have this issue :( Is the search bar getting hidden behind the tableview?
#unforgiven,
did you try this...?
searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 0, 320, 40)];
[self.tableView setTableHeaderView: searchBar];
This manually creates a searchbar and it works. But I'm making some stupid mistake in IB that the SearchBar doesn't show up even though my connections are perfect. :-(
Do update this post if you get the answer...
Ok, I have found how to solve it. In my case, the problem was due to the fact that I was using a controller embedded within the UITabBarController as one of its managed tabs (i.e. as a child).
Removing the controller from the UITabBarController, then adding an UINavigationController to the UITabBarController instead, and finally putting my controller as a child of the UINavigationController solved completely the issue.
I do not understand why this is so (there is no related information in the documentation, as often happens); however, it now works like a charm.
With kind regards.
I had a similar issue
To solve I had to do one additional step to unforgivens answer
In my main nib
1) create a UITabController
2) Then I dragged out a UINavigational Controller into the tab controller
3) Then dragged out a UITableViewController into the NavigationalController as a child
4) Then changed (3) class to be my MyTableWithSearchBarViewController in the inspector - check if the nib name is correct and change this if necessary in the inspector as well
5) I then had to delete the tableView which is automatically created by IB in step (3). Only then would the search bar show correctly...
If in step 3 I dragged out a different controller onto the stage or left the tableView there it would only ever display the table and not the search bar
weird
Tomtrapeze has the right answer if your nib file contains the UITableViewController. But, if you're loading the UITableViewController in code -- such as pushing it on the stack of a UINavigationController -- the solution is a little different.
When initializing the UITableViewController or subclass, you need to use the -initWithNibName:bundle: form of initializer and specify the name of your nib file. Alternately, you could use the standard -initWithStyle: initializer and manually set the nibName property before the view is loaded.
If the nibName property is not set when the view gets loaded, UITableViewController will not use the normal UIViewController nib lookup logic. It will just load a standard UITableView instead.
I recently learned that I could load NIB files with [[MyViewController alloc] initWithNibName:nil bundle:nil]; when the NIB file has the same name as the class (i.e. MyViewController.xib). It turns out that initializing this way causes the 'UISearchBarandUISearchBarDisplayController` to not display. When I initialized the view controller by actually typing out the name of the class, my search bar showed up correctly. I initially thought it had something to do with how I was presenting the view controller but was glad that it wasn't.
PGWSearchViewController *searchVC = [[PGWSearchViewController alloc] initWithNibName:#"PGWSearchViewController" bundle:nil];
searchVC.modalPresentationStyle = UIModalPresentationFullScreen;
searchVC.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentViewController:searchVC animated:YES completion:^{ }];
This is using iOS SDK 5.1 and XCode 4.3.1.
Another possible fix that works for me is to init the UITableViewController with initWithNibName:bundle:
SearchEntryTableViewController* searchEntryTableViewController = [[SearchEntryTableViewController alloc]initWithNibName:#"SearchEntryTableViewController" bundle:nil];
To nest the UITableViewController in a UINavigationController before put it in a UITabBarController doesn't fix the issue for me...