-->

In xCode, I'm getting the error “Loaded the ni

2019-08-12 07:14发布

问题:

I know there are multiple questions asking the same thing, but none of their solutions worked for me.

I made a custom viewController class (Home), and I made a .xib separately. (Effectively, I'll have 2 nibs -- one for the ipad version to load, and another for the iphone).

When I try to instantiate an instance of the home class, I got the error I mentioned in the question.

In the xib file, all I did was drag a viewcontroller object from the library to the screen, and it automatically added a viewcontroller icon below the file's owner and the first responder. I then went into the inspector of the viewController object and changed the class from viewController to Home.

The next step that I've discerned from the solutions is to connect the view outlet from the inspector. I'm not entirely sure what I'm supposed to be connecting it to. The solutions I've found say to connect it to the view icon, but again, all I see is files owner, first responder, and the vc object. It connects to none of them

Here is what I put in the app delegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

 Home *homePage = [[Home alloc] initWithNibName:@"HomeIpad" bundle:nil]; 

UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:homePage];
[self.window addSubview:navController.view];
[self.window makeKeyAndVisible];
return YES;
}

回答1:

I would suggest you to create the Nib using Xcode's File->New->New File->Cocoa Touch->UIViewController subclass->with XIB for user interface (do this twice, once with Targeted for iPad checked). I find this to be the most reliable way of creating and hooking up XIBs.

Even if you want two XIBs for the same controller, create the 2nd VC subclass using any temporary name (say tempvc). Now, rename tempvc.xib to HomeIpad.xib. Right Click on HomeIpad.xib and select Open As->Source code. In the source, search for tempvc and replace it with the name of your controller for all occurrences. Everything should be good now.



回答2:

the problem was that for the file's owner, i hadn't set the class to Home



回答3:

You dont need to drag the view controller. Just drag a UIView instead and connect the File's owner View to this UIView.