I know that the View XIB has a view already in place, but are there any other differences? I've read tutorials which say to create a View XIB, change the class and delete the view, then insert a Table View and remake the connections (File's Owner to the Table View, Table View delegate and datasource back to the File's Owner). Is there any difference in the end result if I follow these steps:
Make an Empty XIB
Change the File's Owner class to one of my custom classes
Add a Table View
Connect the File's Owner to the Table View as its view
Connect the delegate and datasource from the Table View to the File's Owner
No, there is no appreciable difference between an View XIB and an Empty XIB with a Custom View dragged in.
There are no connections in a View XIB initially, so no connections to "remake".
What you describe will work fine, there is no difference between that and creating a View XIB, deleteing the view and continuing from step 2.
You can actually open the .xib files in BBEdit and compare them to see the trivial differences between the XML.
Related
I have an WizardSequenceViewController with an IBOutlet WizardView *_wizardView. In many WizardSequenceViewController.xib file I have the view outlet connected to the File's Owner - WizardSequenceViewController. I have a subview of that view defined with the class attribute set to WizardView. I have connected that WizardView to the outlet in the File's Owner. Finally, in my WizardView.xib I have a UILabel that I have placed in the file to test if the view is being rendered. When I select the WizardSequenceViewController from my tab bar, I see the superview view but not the subview _wizardView. When I set a breakpoint in my -(id)initWithCoder method in my WizardView.m file I see it stop there, so I know that it is calling that initializer (and thus it should be using the xib to load that file). I have tried many iterations and variations to get this thing to work but I can't and I am going crazy. Does anybody have any ideas?
From Apple doc "View Controller Basics, About Custom View Controllers":
The one-to-one correspondence between a view controller and the views in its view hierarchy is the key design consideration. You should not use multiple custom view controllers to manage different portions of the same view hierarchy. Similarly, you should not use a single custom view controller object to manage multiple screens worth of content.
Note: If you want to divide a view hierarchy into multiple subareas and manage each one separately, use generic controller objects (custom objects descending from NSObject) instead of view controller objects to manage each subarea. Then use a single view controller object to manage the generic controller objects.
Maybe you can't do a view-and-subview outlet setup in a view controller. And I'm not sure assigning the subview outlet to a separate NSObject subclass would work either, because how would you present it? Could you write your subview programmatically, using initWithFrame and addSubview, instead of making it an outlet? Or, if you really want to set it up graphically, could you assign it to a separate view controller as owner? Then the top view controller will call presentModal on the sub view controller. Or, if all you need is a UILabel as a subview, just add the label to the main view?
Even I faced a similar issue. But got it resolved by following steps given in the following link. Hope it helps.
http://blog.yangmeyer.de/blog/2012/07/09/an-update-on-nested-nib-loading
I have a tab bar with 4 items and i want to link the .xib files to each item. The problem is that the NIB File Name shows no items, and if i write the .xib for example, StartView without the .xib i get an error. I get the same error when i link the tab item to the view controller class in the Identity Inspector. This is the error:
'NSInternalInconsistencyException', reason: '-[UIViewController _loadViewFromNibNamed:bundle:] loaded the "StartView" nib but the view outlet was not set.'
I had no problem with this in Xcode 3. As far as i know i don't need outlets in my view controller to be able to link a view to a bar item.
Any ideas on how to fix this?
Thanks.
Each .xib has a main view that must be connected to its files owner. Usually when you change the class name of a .xibs owner, the view outlet will become disconnected. So you'll want to first check that the class name of your .xib is the custom controller you have made. You then have to open the .xib and click on the connections inspector. Then just drag and connect the view outlet (labelled as view in the connections inspector) to your view.
Cheers.
Problem solved, but i didn't like the solution. I created the view controller again and selected to create a .xib file along with the controller and that was it. Then i made the usual references from interface builder and it's working. I didn't like it because the .xib file has the same name as the view controller class. If anyone knows how to create the .xib file and the view controllers separately and get it to work, please inform me, i would really appreciate it.
select create new file, objective c, check the make xib file box and in the name of it, add .xib extension to the end of the name
I am developing an application that currently has a View Controller (call it ViewControllerX). The MainWindow.xib file contains the following:
File's Owner UIApplication
First Responder UIResponder
AppX App Delegate myAppDelegate
myViewControllerX myViewControllerX
Window UIWindow
When I look at the MainWindow.xib in Interface Builder it shows View Loaded from "myViewControllerX". The myViewControllerX.xib file for this contains the following:
File's Owner UIApplication
First Responder UIResponder
MainView UIView
Image View (image1.jpg) UIImage View (Inside MainView)
Rounded Rect Button UIButton (Inside MainView)
Rounded Rect Button UIButton (Inside MainView)
Table View UITableView (Inside MainView)
On the Table View, I have the Outlets set to dataSource = File's Owner and Delegate = File's Owner. The Referencing Outlet is set to mTableView which is defined using IBOutlet in XCode. When I run this all works fine and the table gets populated.
When the users clicks an Item within the Table View I want a new view to slide into place, which also contains a TableView (basically the first View Controller shows an overview list, when the user clicks on an Item I want it to show the details of the choice). In the myViewControllerX.m file, I have the following code:
- (void)tableView:(UITableView*)theTableView didSelectRowAtIndexPath:(NSIndexPath*)theIndexPath
{
[self.navigationController pushViewController:self.mViewControllerY animated:YES];
}
I have created a ViewControllerY and have the following files:
ViewControllerY.xib, ViewControllerY.m and ViewControllerY.h.
In the .xib file for ViewControllerY, I have:
File's Owner UIApplication
First Responder UIResponder
Table View UITableView
When I run this it works (so the code and the hook-ups in Interface Builder are ok). The Problem is it takes over the whole of the view whereas want I want it to have an Image (and other UI objects) as well as a Table View. I've tried changing the UITableView of ViewControllerY to be of type UIView and then adding an UIImageView and UITableView inside of the UIView in a similar way to ViewControllerX but I can't get it to work and now I'm not sure what to do! So, my question is, how do I go about Implementing this? I'd like to be able to have it setup in Interface Builder, so how to I change it to handle this? e.g How do I hook up the dataSource, Delegate and Outlets etc.
Thanks in Advance for any help in this. I've tried all kinds of things but I just can't seem to get it to work. I'm sure I'm almost there and must be missing something that is very obvious!
All the Best
Dave
Your UINavigationController uses a content view that more or less completely covers your device screen. And this view is used for your main view. If you ask this UINavigationController to push another view, he uses this very same view to swap in the ViewControllerYs view which also covers the complete screen.
To gain the behavior you need, you should create a second "embedded" Navigation Controller instance which is responsible only for that part that is initially by your table view. Display your table view as the main view and then ask this Navigation Controller to swap in your other views into this part of the screen.
After taking a step back and doing a bit more digging, I came up with the answer.
High Level Explanation.
ViewControllerY needs to be of class UIViewController, not UITableViewController. The ViewControllerY : UIViewController definition in the .h file should include the Table View Delegate and the Data Source protocols. A member of the ViewControllerY has to hold the UITableView* - call it mTableView and should be defined as an IBOutlet.
The table view delegate methods should then use self.mTableView to access the table.
In Interface Builder, the UIView Object needs to have an Outlet "view" hooked up to the File's Owner and the Table View inside the UIView needs to have the dataSounce and the Delegate set to File's Owner and the mTableView Outlet also needs to be hooked to the File's Owner.
Low Level Explanation.
in ViewControllerY.h do the following:
#interface ViewControllerY : UIViewController <UITableViewDelegate,UITableViewDataSource>
{
UITableView* mTableView;
}
#property (nonatomic, retain) IBOutlet UITableView* mTableView;
In ViewControllerY.m, in the Table View Delegate methods, access the mTableView like so:
myNewCell = (UITableViewCell*)[self.mTableView dequeueReusableCellWithIdentifier:myCellIdentifier];
(obviously you need to define and handle all the other Table View Methods in the normal way.
ViewControllerY.xib should have the following in it:
File's Owner ViewControllerY
First Responder UIResponder
View UIView
Image View UIImageView
Table View UITableView
Control Drag from the File's Owner to the View and select "view".
Control Drag from the Table View to the File's Owner and select Data Source.
Control Drag from the Table View to the File's Owner and select Deligate.
Control Drag from the File's Owner to the Table View and select mTableView.
That's it! You can then add other UI objects inside the UIView and hook them up as appropriate in the normal way. I'm not sure if this is the only or best solution but is works and I'm happy with it.
Hope this helps someone with the same or similar problems. It's a bit confusing til you get the hang of it.
Hi guys I have a table view but I don't want it to fill al the screen let's say I want it to be 320x420.
Well I want to put under the table view an image like a bar an image that would be 320x60.
The first time I tried this the image went down to the section footer of the table view and I wasn't able to see it until I get to the bottom of the table view.
After that I tried on the IB to resize the tableview and put under it the image, but when I tried it on the simulator the image didn't show up. I thought I was because the File's Owner view outlet was connected to the table view, so after that I disconnected with it and connected with the view that contained the table view, and when I tried in the simulator it crashed.
So I was looking on internet but still I can't find the answer. I don't know if you actually understood well my problem, cause my english kinda sucks.
Best Regards
Carlos Vargas
You don't need to connect the UIImageView to anything in the view. It will get loaded and placed automatically. The only reason you might want to connect it to anything is if you want to change something in it programmatically in the controller, and in that case you'd connect it to an IBOutlet in the controller.
I think what you need to do is create a UIView, then put the table view and the UIImageView inside the UIView. In IB it will look something like:
__
\/View UIView
Table View UITableView
Image UIImageView
You should be able to resize the subviews the way you want.
In the controller (File's Owner) make sure you have outlets for both View and Table View, then connect those views to the corresponding outlet. (Really you don't need to connect Table View to anything unless you need to directly configure it from the controller somehow. Setting the delegate and datasource properly should be enough.) Make sure to connect delegate and dataSource in the table view to the controller as usual. Make sure the view outlet in file's owner is connected to View and not TableView!
Your controller doesn't need to be a subclass of UITableViewController, it just needs to support the protocols. In fact, in this case using UITableViewController might not work because it may get "confused" if view is not set to a table view.
I just did this and it worked. In my case, though, I put an info button over the table view and did some other stuff.
Hope that helps.
I was trying to follow the Table View Programming Guide for iPhone OS but was having trouble creating a new Table View Controller that loads its data from a nib file.
Specifically, I was trying to follow the steps in this part:
If you prefer to load the table view
managed by a custom table-view
controller from a nib file, you must
do the following:
In Interface Builder, create an empty Cocoa Touch nib file (File >
New).
Drag a UITableViewController object from the Interface Builder
Library into the nib document window.
Save the nib file in your project directory under an appropriate name
and, when prompted, select your
project to have the nib file added to
it.
Select Table View Controller in the nib document window and open the
Identity pane of the inspector. Set
the class to your custom table-view
controller class.
Select File’s Owner in the nib document window and set its class
identity to the custom table-view
controller class.
Customize the table view in Interface Builder.
Select the table-view controller in the nib document window, open the
Attributes pane of the inspector, and
enter (or select) the name of the nib
file in the Nib Name field.
So I created a new UITableViewController subclass in Xcode (called "MyTableViewController"), then I went into IB and followed those steps. I made sure to link up all the Class attributes to the same name as the UITableViewController subclass I made in Xcode like it says in the steps.
But now I get the following warning in IB:
"My Table View Controller" has both its
"View" and "Nib Name" properties set.
This configuration is not supported.
When I run the application and push the table view controller, it appears but it seems like nothing is being loaded from the nib file at all (e.g. I set the alpha to 0 instead of 1).
Any idea as to what I'm doing wrong?
Thanks for the help.
Here's some more information that might help you understand the situation better.
I noticed a few differences between creating a UITableViewController with the template (e.g. by creating a new Navigation-based Application) vs. creating one yourself (e.g. following the steps above). I'm going to refer to each as TemplateNib and CustomNib, respectively, to make it easier to understand the differences.
In TemplateNib, it has the following objects in the document window:
File's Owner
First Responder
Table View
In CustomNib, it has the following objects in the document window:
File's Owner
First Responder
My Custom Table View Controller
Table View
Another difference is in the File's Owner links...
TemplateNib's File's Owner:
Outlets
tableView -> Table View
view -> Table View
Referencing Outlets
dataSource -> Table View
delegate -> Table View
CustomNib File's Owner:
Outlets
view -> (nothing)
CustomNib My Table View Controller:
Outlets
view -> Table View (this is grayed out so you can't delete it)
Referencing Outlets
dataSource -> Table View
delegate -> Table View
Update:
I tried to mimic the .xib file that is created by the template by following these steps:
Created an empty file in Interface Builder.
Set the File's Owner to the class that inherits from UITableViewController.
Added a Table View to the document window.
Set the Table View's dataSource and delegate to File's Owner.
Set the File's Owner view to the Table View.
Added a tableView propery in the Identity pane of type UITableView.
Set the File's Owner tableView property (which I just created) to the Table View.
However, this still seems like it is not loading it from the NIB file. (I also never set the name of the NIB file anywhere though... is there anyplace I need to set it or does it look for one with the same name?).
I then tried overriding initWithNibName to load from the name of the nib file, and now it does seem to load it from the nib file. However, if I look at the .m file of the TemplateNib table view controller, it doesn't need to override this method, why is that? I still think I am doing it the wrong way cause the Programming Guide didn't mention anything about doing it this way.
Update:
I tried comparing the two .xib files using a diff tool, the only significant difference between the two seems to be:
<string key="superclassName">UITableViewController</string>
// and:
<reference key="NSSuperview"/>
I don't see any reference to the Nib file in the original file anywhere, are there any other files I should check?
Update:
It seems like the thing that makes TemplateNib load from the nib file is that in the MainWindow.xib (default name given by the template), the RootViewController is added with the NIB Name property having the value "RootViewController". Additionally, its class is set to "RootViewController".
I tried putting a breakpoint in both initWithNibName:bundle: and initWithStyle: on the RootViewController, however, it never gets to there. I'm kinda wondering how the TableViewController is created when you set it up in the MainWindow.xib like that.
I also tried adding the my custom table view controller to MainWindow.xib, setting the class and nib names in hopes that it will load it from the nib file I specified, but it doesn't even call iniWithNibName.
Create a TableViewController in Xcode.
Create an empty nib file in Interface Builder.
Set the File's Owner Class property to the TableViewController from step 1.
Add a TableView to the empty nib file.
Set the File's Owner view property to the TableView from step 4.
Customize the TableView in IB as you want.
Override the initWithNibName:bundle: method in Xcode for the TableViewController you created and use code similar to the following:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if (self = [super initWithNibName:#"MyNibName" bundle:nibBundleOrNil]) {
// Custom initialization
}
return self;
}
I had this same problem last night and found this post while trying to search for the answer. I ended up solving it.
Basically I had opened up the wrong XIB file (I hoped main_window.xib, not the view controllers xib)
I cut all the controls from my main xib, pasted them into the controllers xib, realigned everything, reconnected all the outlets/actions and the warning went away :)
Hope this helps someone :)
Instead of doing all that, I would use the "New File" iPhone UI template to create a TableViewController with xib file option checked. Then you get a controller and xib file all wired together properly.
Eagle, when you create a new file, select the "UIViewController subclass" icon. There's a checkbox to make it a UITableViewController subclass just above the checkbox to include XIB file.
You've got two places where your UITableViewController shows up in Interface Builder.
(1) It shows up in the nib with the controllers own name.
(2) It shows up as a controller object in the nib of another object, usually the MainWindow.
Your problem is at (2). There are two ways to set the tableview for a UITableViewController in Interface builder. First, you can create a UITableView under the controller in the MainWindow and connect that to the controller's view property. Secondly, you can bring up the inspector in the attributes pane and in the popmenu listed "NibName" select the name of the controllers nib.
You can't use both systems at once because the first loads a view from the MainWindow nib file and second loads a completely unrelated view from the controller's separate nib file.
This is one of those maddening errors that using Interface Builder makes so hard to track down.