iOS who is file owner of subview? - ios

I am newbie in iOS development and I have a problem to understand file owner logic.
I have this situations:
I have two views (View_iPad, View_iPhone) and their controllers (ViewController_iPad, ViewController_iPhone). Based on a device where app is launched, particular view and its controller is used. Then, I add a subview from xib file to this view (it doesn't matter if iPad or iPhone view is used). Let's say that there is a different position of this subview in iPad and iPhone view. This subview contains some labels, so I have to create IBOulets in subviews controller.
If I understand, according to apple's logic, file owner of view is a controller (file which has access to this view and can communicate with it). So each of 3 views (iPhone, iPad, subview) has its own controller, it means that these controllers are file owners of these views.
But when I load subview with code:
NSArray *nibContents = [[NSBundle mainBundle] loadNibNamed:#"subview" owner:self options:nil];
and add it as a subview to iPad/iPhone view, there is a problem because this subview contains IBOutlets for labels and this labels are in subview controller and not in a iPhone/iPad controller.
This error is shown then:
Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<__NSCFConstantString 0x7ab4> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key view.'
How can I solve this? I tried to put as a owner #"subviewController" but it didn't help. Thank you.
EDIT!!:
So I was trying to solve this but still it doesn´t work. Here is a new situation:
Subview controller is owner of the subview but custom class of the root view is a class (derived from UIView) which contains outlets.
If I have in iPad/iPhone controller owner:#"subviewController", Igot this error:
Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<__NSObject 0x715bf30> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key view.'
Where the view is a property from subview controller. Now tell me where is the problem. I am really desperate!
Or If I have owner:self, some window with instructions is opened with EXC_BAD_ACCESS error.

So first of all, if you have a view controller created from xib/storyboard then the view controller owns his view. If you create a custom view using xib files then the files owner will be the custom class(a subclass of UIView) but when you are creating custom views with xib files you have to link the IBOutlest to the UIView not to the files owner like you are doing when you have a view controller.
Second, your problem is not generated by the owner your problem si generated by an outlet which is wrongly linked, probably you linked an view to an outlet which is a string, so check all your outlets and their corresponding views.

The File's Owner icon in the Interface Builder-like part of Xcode (since IB doesn't exist separately anymore) is a proxy for the object that is specified as the owner when the nib is loaded. Usually, that's the object that loads the nib, typically the application or a view controller.
When you say:
NSArray *nibContents = [[NSBundle mainBundle] loadNibNamed:#"subview" owner:self options:nil];
then File's Owner will be whatever object self is in the line above. If you want an object other than the one that contains the line above to be the owner, specify that object in the owner: parameter.

Related

Issue with Uicollectionview for nib but didn't get a UICollectionView

I am new for IOS development , and I try to use UIcollectionView to show the photo.
I create a xib file from New file -> iOS -> User Interface -> View , and add a Collection View like the following picture.
But I got the following error log when it crash.
Terminating app due to uncaught exception 'NSInternalInconsistencyException',
reason: '-[UICollectionViewController loadView]
loaded the "AITLocalGridViewController" nib but didn't get a UICollectionView.'
And it seems didn't call viewDidLoad
I have search for some information , some information indicated that I dint not connect the collection view in xib file
But I don't understand what it mean.
Could some one help me and teach me what to do ?
Make sure that you have connected UIcollectionview IBOutlet with “collectionview “ that you declared in your program. And also you should make delegate&data source connection(For making this, click on th Uicollectionview in the xib. Select Connection Inspector. Where you can see delegate and datasource.By pressing ctrl button drag and connect it to File’s Owner.)
Just remove the view in Storyboard and use the UICollectionView as direct child.
Make sure UICollectionViewController's "view" outlet is set to a UICollectionView. Setting the "collectionView" to a UICollectionView is not enough for a UICollectionViewController.
At the risk of stating the obvious, also make sure that the UICollectionView is inside a UICollectionViewController. This can be overlooked when you do something like adding an embedded view into your project, where the default embedded view is the standard view controller.

Loading UIView from NIB

I have a view controller V which is part of a story board. On its viewDidLoad method it attempts to load a few views from a NIB (not a storyboard). The idea is to "assemble" some of the view using these "subviews".
However, when I try to load a view like this:
NSArray *bundle = [[NSBundle mainBundle] loadNibNamed:#"x"
owner:self options:nil];
I'm getting an exception saying 'this class is not key value coding-compliant for the key yyy.'
This is because the view in the NIB has a custom view controller, which has an outlet to a component in this view, which is connected in the NIB properly.
What am I doing wrong here?
right click on your fileOwner viewController in interface builder and then delete yyy outlet, you have an outlet connection, which is not existed or deleted
Update screenShote added
Look at this screenshot the button has 2 outlets, but yyyy is not existed in the viewController. you should delete this outlet

How can I create view controller that is defined in XIB?

Sorry for perhaps noob question, guys. I'm new to iOS development.
How can I create view controller that is defined in XIB ?
When you create new class inherited from UIViewController in Xcode via "New File" and mark with XIB checkbox it creates xib file that doesn't have viewController element. But rather it has placeholder which points to UIViewController which is defined in the code.
But I see that you can create controller itself in Interface Builder and specify customClass for it.
Unfortunately it doesn't work for me. I get loaded the "EmbeddedViewController" nib but the view outlet was not set error in run-time. But I believed view property must be set by resource loader automatically since everything is defined via XIB. It works that way for TableView and its cells for example.
Could you please provide example how to do it ?
I created sample project: https://github.com/cppexpert/SampleWithNib
The controller in question is EmbeddedViewController
Create one Xib per UIViewController
Each Xib have File's Owner object for you to set UIViewController class there. Click File's Owner and choose EmbeddedViewController on it's class
Then drag main UIView not UIViewController class to there, then hook up this view with file's owner as view. UIViewController just use to drag to StoryBoard base project.
https://github.com/lequysang/gitfiles02/blob/master/SampleWithNib-master.zip
Turned out these controls exist in IB for Storyboard projects.
When you create a view controller with xib via "new file", Xcode generates an UIView and connects it with view outlet automatically. Seems like you changed something after Xcode generated the xib file. In that case you need to connect a view to the view outlet manually.
If you create a new view in xib.set the file owner to your custom class.
Or if you simply want to create a view.
Then
NSArray* test1 = [[NSBundle mainBundle] loadNibNamed:#"View" owner:self options:nil];
self.myViewFromNib = [test1 objectAtIndex:0];
Where myViewFromNib is your view object and "View" is your nib name.

Can you use a view controller created for a xib with storyboards?

We have a framework that we generally use with our iOS projects that includes all of our login information and stuff like that. It was created before iOS5 and therefore did not use storyboards. We are now planning to move into storyboards. I created the initial view Controller, exactly like the xib in another project. Then I set the view controller in the Identity Inspector and hooked in all of the outlets and actions from the Connections Inspector. However, when I run the project, I get the following error:
2012-09-14 08:46:15.366 Mixable[3923:f803] Unknown class LoginViewController in Interface Builder file.
2012-09-14 08:46:15.460 Mixable[3923:f803] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<UIViewController 0x6851200> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key loginBtnFacebook.'
*** First throw call stack:
(0x14b1022 0xeb1cd6 0x14b0ee1 0x9c2022 0x933f6b 0x933edb 0x94ed50 0x23671a 0x14b2dea 0x141c7f1 0x23526e 0xdb1fc 0xdb779 0xdb99b 0x3a401 0x3a670 0x3a836 0x4172a 0x12596 0x13274 0x22183 0x22c38 0x16634 0x139bef5 0x1485195 0x13e9ff2 0x13e88da 0x13e7d84 0x13e7c9b 0x12c65 0x14626 0x201d 0x1f85 0x1)
terminate called throwing an exception
and if I remove the connection to the loginBtnFacebook, I just get it for the next outlet that I have hooked up, and that continues until I remove all outlets.
Is it possible to use the same View Controllers or will we need to rewrite all of them?
In answer to your question, the view controllers are largely the same (except which init methods are used, e.g. you obviously don't use initWithNibNamed with storyboards, but this seems unrelated to your problem here).
So, a couple of questions related to your error, which is the typical symptom of outlet linking issues:
Have you configured your scene's view controller class? Click on the view controller button at the bottom of the scene, click on on the "Identity inspector" tab button at the top of the right panel, and make sure you specify your view controller class.
Are you IBOutlet's hooked up correctly? When you click on the "Assistant Editor" in the right side of the toolbar, you should see the .h file for your class show up at the bottom. Do you have solid black dots next to the IBOutlet definitions ensuring that everything is hooked up?
This is unlikely to be the issue, but have you included the view controller's .m file in the "Compile Sources" section of "Build Phases"?

What are File Owner and First Responder in iOS - Xcode?

What are File Owner and First Responder in iOS - Xcode?
The File Owner is an instantiated, runtime object that owns the contents of your nib and its outlets/actions when the nib is loaded. It can be an instance of any class you like - take a look at the identity tab of the tool palette.
File Owner is the main link between your application code and the contents of the nib file.
For example, consider you have a UIViewController subclass with an IBOutlet for a UILabel. In interface builder the File's owner will be set to the same class as your UIViewController. When your nib is loaded at runtime, the bindings of outlets and actions defined in your nib are bound to the instance of your view controller, as your view controller is the owner.
Nibs are loaded using:
[[NSBundle mainBundle] loadNibNamed:#"NibName" owner:nil options:nil];
The owner parameter is particularly important. That's the runtime instance of a class that owns the contents (outlets, actions and objects) of the nib being loaded.
Hopefully that's clear. To see this at work create a brand new iPhone project with a view controller. Open the Nib file and take a look at the identity tab.
First responder is simply the first object in the responder chain that can respond to events. The responder chain is a runtime collection (or more accurately a hierarchy) of objects that can respond to an event. For example, consider you have a window with a view and on that view is a text field.
If that text field has focus it's known as the first responder in the chain. So if you send a message to the first responder it'll be sent to the text field first. If the text field can't handle the message it'll be sent to the next responder. And the next. And the next, until you get to the end of the responder chain or something has consumed the event (iirc).
The responder chain is worth reading about - hit apple's documentation for more information.

Resources