BMN
BMN

Reputation: 8508

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.

enter image description here

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 ?

Upvotes: 12

Views: 12034

Answers (9)

S L
S L

Reputation: 11

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.

e.g.

class LabsViewController: UITableViewController {

Upvotes: 1

gmogames
gmogames

Reputation: 3081

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.

Upvotes: 0

de.
de.

Reputation: 8457

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...

Upvotes: 0

Anton Plebanovich
Anton Plebanovich

Reputation: 1516

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.

Upvotes: 0

Mike Sukmanowsky
Mike Sukmanowsky

Reputation: 4477

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 :).

Upvotes: 18

ross_t
ross_t

Reputation: 511

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.

Upvotes: 25

Pochi
Pochi

Reputation: 13459

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.

Upvotes: 4

Mihriban Minaz
Mihriban Minaz

Reputation: 3039

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

Upvotes: 4

MrBr
MrBr

Reputation: 1916

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.

Upvotes: 0

Related Questions