3

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.

DanielH
  • 953
  • 10
  • 30
  • Two questions: 1) In which class is the ` NSArray *nibContents = ` line executed (i.e., what is the value of self) and 2) Where is the exception fired? To find out set an exception breakpoint on 'throw'. – NSSplendid May 16 '13 at 12:18
  • 1) Depends on which device the app is launched. Self is either ViewController_iPad or ViewController_iPhone. 2) Exception is fired in the line with loadNibNamed. – DanielH May 17 '13 at 07:35

2 Answers2

3

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.

danypata
  • 9,895
  • 1
  • 31
  • 44
  • 1) > To what UIView I have to bind outlets? File owner in this case is subviewController and it contains outlets and I just bind them to UIViews in IB – DanielH May 17 '13 at 07:23
  • 2) > - I don't understand what you mean by a string. I know that this is a stupid question but I am a newbie and still don't understand this issue. – DanielH May 17 '13 at 07:26
2

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.

Caleb
  • 124,013
  • 19
  • 183
  • 272
  • OK, the code with loadNibNamed is in iPhone/iPad controller. If a owner is set to self, it means that owner is iPhone/iPad controller. But in this controller there aren't IBOutlets for labels in a subview. And as I wrote, I tried to set owner as subviewController and not self but it didn't help. And I can't put IBOulets into iPhone/iPad controller because then I can't bind labels to IBOutlets that are in a different file. – DanielH May 17 '13 at 07:15
  • Take a look at File's Owner in the nib file. What is the type of File's Owner in the inspector? You need to make sure that the object you specify as the owner when you load the nib is an object of that same type. Also, whatever class you specify as File's Owner's type needs to have a `view` property, or otherwise be KVC compliant for the key `view`. That's exactly what the error is telling you: the owner isn't KVC compliant for a key named `view`. – Caleb May 17 '13 at 14:39