3

I have a xib file that includes a view controller but I have the same problem as this link: problem

In this answer they can easily set outlet because view has a circle that is clickable.But in my case the view outlet is not even clickable.So I can't set the outlet.What to do now?

Community
  • 1
  • 1
Okhan Okbay
  • 1,374
  • 12
  • 26
  • Loading xib like this solved it : `MyViewController *vc = [[MyViewController alloc] init]; NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"myNibName" owner:self options:nil]; vc = [nib objectAtIndex:0];` After this I did not need to set outlet for view. – Okhan Okbay Dec 11 '14 at 23:03
  • I was using `MyViewController *vc = [[MyViewController alloc] initWithNibName:@"myNibName" bundle:nil];` and it was expecting me to set an outlet for the view – Okhan Okbay Dec 11 '14 at 23:05
  • Can you share your project or xib file with .h and .m files? You 100% misconfigured something in Interface Builder – Vitalii Gozhenko Dec 11 '14 at 23:16

2 Answers2

5

The correct way to use initWithNibName:: is to have a "View" IB document where you have the desired VC view outlet as a root element. You need to set the "File's Owner" "Class" to your UIViewController subclass and connect it's view outlet: enter image description here

Also, don't present modal VC from self at viewDidLoad: at the time of this method execution the VC is often not yet presented itself, viewDidAppear: is more fitting for such tests.

A-Live
  • 8,904
  • 2
  • 39
  • 74
  • The reason I wrote it in the `viewDidLoad` was to create a quick reference project to show what I meant in the question.But thank you for the "it must be view to use `initWithNibName` " answer.It makes sense. – Okhan Okbay Dec 12 '14 at 11:43
3

I had this same problem, but neither this nor any other solutions seemed to work - what I wound up doing was setting the custom class to UIViewController, linking the view as described in the question link, and then changing the class back to the actual custom class name I had intended. The link stayed and everything worked from that point on.