xcode IB - 无法为视图控制器分配类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11950667/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 01:14:02  来源:igfitidea点击:

IB - Can't assign class for a View Controller

iphoneiosxcodeuiviewcontrollerinterface-builder

提问by Nugget

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 :

我对 IOS 开发很陌生,我在这里面临一个我以前从未遇到过的问题。这是我所拥有的:

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.

我创建了一个项目,添加了一些附加到他们自己的类的 ViewController。但是现在,我只是在情节提要中添加了一个新的 ViewController。然后我创建了一个新的 Objective-C 类(它是 UIViewController 的子类)。问题是在 IB 中,我无法将 ViewController 链接到新创建的类,因为我根本没有 IB 提供的列表中的类(见下图)。我的新课程名为MapShownViewController,但正如您在图像上看到的,它不可用。

enter image description here

在此处输入图片说明

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

它适用于我所有的其他课程,但不适用于这门课程。

Here's the MapShownViewController.h :

这是 MapShownViewController.h :

#import <UIKit/UIKit.h>

@interface MapShownViewController : UIViewController

@end

And the MapShownViewController.m :

和 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 ?

有人可以解释一下我做错了什么吗?

回答by ross_t

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.

我遇到了这个确切的问题,这让我很生气。我第一次注意到它是在将 XCode 从 4.1 升级到 4.4 之后。我有一个现有的项目,我一直在旧版本的 XCode 上工作,我在 4.4 中继续工作。我完全按照您所做的去做,并在故事板中创建了一个新视图,然后创建了子类文件,但是该类在 IB 的自定义类下拉列表中不可用。经过多次谷歌搜索和沮丧之后,我求助于退出 Xcode(完全退出,而不仅仅是关闭),然后重新启动它。然后就像魔法一样,一切都开始工作了,新类在 IB 自定义类下拉列表中立即可用。

回答by Mike Sukmanowsky

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 customClassattribute:

遇到同样的问题,重新启动 XCode 没有帮助。您始终可以右键单击情节提要并选择Open As > Source Code。在 XML 文件中找到您的视图控制器元素并添加一个customClass属性:

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

Manual override FTW :).

手动覆盖 FTW :)。

回答by Mihriban Minaz

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

检查您的项目设置。xcode->目标->构建阶段->编译源您的视图控制器的实现文件必须添加到此列表中。

回答by Pochi

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

为此,您必须确保以下几点:

1) The element added to the storyboard is an UIViewController

1)添加到storyboard的元素是一个UIViewController

2) The class you defined has the UIViewControlleras its superclass

2) 你定义的类有UIViewController作为它的超类

@interface MapShownViewController : UIViewController

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

3)该类正在项目中正确构建。

回答by S L

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 {

回答by MrBr

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.

检查您是否在新班级中选择了正确的超班级。有时您会创建一个由 UITableVIewController 继承的视图控制器。这个不能应用于 Storyboard 中的 ViewController 模式。

回答by Anton Plebanovich

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.

我必须设置我的自定义类,它从 UIViewController 继承并充当 UITableViewController 的表视图控制器。我做了一个简单的技巧,只是将我的自定义类继承更改为“:UITableViewController”,然后我可以自由地将这个类设置为控制器。当然,在设置之后,我将继承改回来了。

回答by de.

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.

我在 Mac OSX 应用程序上使用 Xcode 7.1.1 遇到了同样的问题。我尝试了其他答案中的所有建议 - 没有成功。

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

最后我删除了我的视图控制器文件并创建了全新的文件。
然后它突然起作用了......

回答by gmogames

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.

我注意到使用 Command+Shift+O 搜索我的自定义视图控制器文件被找到了,但是我看不到文件在哪个文件夹中,所以我注意到由于某种原因我的自定义类从项目中丢失了但是仍然在搜索中找到。

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

我所要做的就是再次将文件移动到项目中,瞧!

Hope this can help someone in the future.

希望这可以帮助将来的人。