简体   繁体   中英

IB - Can't assign class for a View Controller

I'm quite new to IOS developpement, and I'm facing a problem here that I haven't encountered before. Here's what I have :

I created a project, added some ViewControllers attached to their own classes. But now, I just added a new ViewController in the storyboard. Then I created a new Objective-C class (with is a subclass of UIViewController). The problem is that in IB, I can't link the ViewController to the newly created class, as I simply don't have the class in the list provided by IB (see the image below). My new class is called MapShownViewController , but as you can see on the image, it's not available.

在此输入图像描述

It worked well for all my other classes but not for this one.

Here's the MapShownViewController.h :

#import <UIKit/UIKit.h>

@interface MapShownViewController : UIViewController

@end

And the MapShownViewController.m :

#import "MapShownViewController.h"

@interface MapShownViewController ()

@end

@implementation MapShownViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end

Can someone explain me what I made wrong please ?

I had this exact problem and it drove me mad. I first noticed it after upgrading XCode to 4.4 from 4.1. I had an existing project that I had been working on in the older version of XCode, and I continued to work on it in 4.4. I did exactly what you did and created a new View in the story board and then created the sub-class files, but the class just was not avaiable in the Custom Class dropdown in IB. After much Googling and frustration I resorted to Quit Xcode (complete quit, not just close) and then re-start it. Then as if by magic it all started working and the new class was immediatley available in the IB custom class dropdown.

Ran into the same issue and restarting XCode was no help. You can always right click the storyboard and select Open As > Source Code . Find your view controller element in the XML file and add a customClass attribute:

<viewController title="Login" id="TJH-Bg-q4u" customClass="XYZLoginViewController" sceneMemberID="viewController">
   ...
</viewController>

Manual override FTW :).

Check your project settings. xcode->targets->build phases->compile sources your viewcontroller's implemantation file must be added to this list.

For this to work you have to make sure of the following:

1) The element added to the storyboard is an UIViewController

2) The class you defined has the UIViewController as its superclass

@interface MapShownViewController : UIViewController

3) The Class is being correctly built in the project.

I've been running into this issue and tried all other solutions posted here.

The thing that worked for me was to set the correct super class after creating a custom class. Then you should be able to find and select it from the class dropdown.

eg

class LabsViewController: UITableViewController {

Check if you did choose the correct super class in your new class. Sometimes you create a view controller inherited by a UITableVIewController. This one can't be applied to a ViewController pattern in the Storyboard.

I had to set my custom class which inherit from UIViewController and act as table view controller to UITableViewController. I made simple trick and just changed my custom class inheritance to ": UITableViewController" and then i can set this class freely to controller. Of course after it was set i changed inheritance back.

I had the same issue with Xcode 7.1.1 on a Mac OSX app. I tried all the suggestions from the other answers – no success.

Finally I deleted my view controller files and created brand new ones.
Then it suddenly worked...

Just adding another possible solution that helped solve my problem since the others didn't.

I noticed that searching for my custom View Controller file using Command+Shift+O it was being found, but I couldn't see in which folder the file was, so I noticed that for some reason my custom class was missing from the project but still being found in the search.

All I had to do was to move the files to the project again and Voila!

Hope this can help someone in the future.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM