ios 已加载笔尖但未设置视图插座 - Swift 版

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26069771/
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-08-31 02:50:17  来源:igfitidea点击:

Loaded nib but the view outlet was not set - Swift edition

iosobjective-cswiftxibnib

提问by cowlinator

I have a project that is all in Objective C, except for my view controller, which is in Swift.

我有一个项目全部使用 Objective C,除了我的视图控制器使用 Swift。

When I run it, i get the error

当我运行它时,我收到错误

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[UIViewController _loadViewFromNibNamed:bundle:] loaded the "..." nib but the view outlet was not set.'

由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“-[UIViewController _loadViewFromNibNamed:bundle:] 加载了“...”笔尖,但未设置视图出口。

So I opened up my nib file, look at "File's Owner", and I see that the view does not even show up as an outlet at all.
For my old view controller (objective c), the view outlet does show up.

所以我打开了我的 nib 文件,查看“文件的所有者”,我看到该视图根本没有显示为出口。
对于我的旧视图控制器(目标 c),视图插座确实出现了。

In my Swift view controller, I tried overriding the "view" variable from UIViewController in order to force it to be an @IBOutlet, but it complained about the "view" variable being of type UIView, complained about UIView?, and complained about UIView!.

在我的 Swift 视图控制器中,我尝试覆盖 UIViewController 中的“view”变量以强制它成为 @IBOutlet,但它抱怨“view”变量是 UIView 类型,抱怨 UIView?,抱怨 UIView !。

Here are simplified versions of

以下是简化版

my AppDelegate.h

我的 AppDelegate.h

#import <UIKit/UIKit.h>

@class MyViewController;
@class MyViewControllerSwift;

@interface MSAppDelegate : UIResponder <UIApplicationDelegate>
{
}

@property (strong, nonatomic) UIWindow *window;

@property (strong, nonatomic) UIViewController *viewController;

@end

AppDelegate.m

AppDelegate.m

#import "MyAppDelegate.h"

#import "MyViewController.h"
#import "MySwift-Swift.h"
#import <UIKit/UIKit.h>

@implementation MyAppDelegate

static BOOL USE_SWIFT_VIEW_CONTROLLER = YES;

- (void)dealloc
{
    [_window release];
    [_viewController release];
    [super dealloc];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    // Override point for customization after application launch.

    id viewControllerPtr = nil;
    if(USE_SWIFT_VIEW_CONTROLLER)
    {
        viewControllerPtr = [MyViewControllerSwift alloc];
    }
    else
    {
        viewControllerPtr = [MyViewController alloc];
    }

    UIViewController* vController = nil;
    if(USE_SWIFT_VIEW_CONTROLLER)
    {
        vController = [[viewControllerPtr initWithNibName:@"MyViewControllerSwift" bundle:nil] autorelease];
    }
    else
    {
        vController = [[viewControllerPtr initWithNibName:@"MyViewController" bundle:nil] autorelease];
    }

    self.viewController = vController;
    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];

    return YES;
}

ViewController.swift

视图控制器.swift

import Foundation
import AVFoundation

@objc class MyViewControllerSwift : UIKit.UIViewController {

    var player : AVFoundation.AVAudioPlayer?;

    @IBOutlet weak var myTextView : UITextView!;

    required init(coder aDecoder : NSCoder) {
        super.init(coder:aDecoder);
    }

    override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) {
        super.init(nibName:nibNameOrNil, bundle:nibBundleOrNil);
    }

    override func viewDidLoad() {
        super.viewDidLoad();

        println("Using MyViewControllerSwift");
    }

    deinit {
        //TODO
    }
}

What do I need to do to get my view to display?

我需要做什么才能显示我的视图?

Thanks.

谢谢。

(Yes, this is a similar question to Loaded nib but the view outlet was not set - new to InterfaceBuilderbut the view outlet does not show up. )

(是的,这是与Loaded nib类似的问题,但未设置视图插座 - InterfaceBuilder 的新功能,但未显示视图插座。)

回答by user3677173

  • First - set custom class in your nib file (File's Owner -> third icon -> custom class : YourViewController )
  • Second - the last icon in file's owner -> link (drag) the "view" property to the interface view
  • 首先 - 在你的 nib 文件中设置自定义类(文件的所有者 -> 第三个图标 -> 自定义类:YourViewController)
  • 第二 - 文件所有者中的最后一个图标 -> 将“视图”属性链接(拖动)到界面视图

Init your ViewController like so:

像这样初始化你的 ViewController:

YourViewController(nibName: "YourViewName", bundle: nil)

It will work. Don't do any manipulations with View.

它会起作用。不要对 View 进行任何操作。

回答by Travis M.

If you have tried everything and you still get this error, try re-creating the class file from scratch but remember to select the "Also create XIB file" check box. This will auto link up a few items that are not linked when creating these files separately. After this is created, you can likely cut and paste everything onto the new XIB and it should work fine.

如果您已经尝试了所有方法但仍然出现此错误,请尝试从头开始重新创建类文件,但请记住选中“同时创建 XIB 文件”复选框。这将自动链接一些单独创建这些文件时未链接的项目。创建后,您可以将所有内容剪切并粘贴到新的 XIB 上,它应该可以正常工作。

I am finding this issue specifically with creating files separately in Swift.

我在 Swift 中单独创建文件时发现了这个问题。

回答by user5553647

I had the same problem , my 2nd view on my view controller couldn't load . I changed the view's name in property(as I had previously given "View"). Your existing view controller already had references to the view on top of it with the same name(View). Hence the error . Try changing the outlet names . It worked perfect in my case .

我遇到了同样的问题,我的视图控制器上的第二个视图无法加载。我在属性中更改了视图的名称(正如我之前给出的“视图”)。您现有的视图控制器已经引用了它上面具有相同名称(视图)的视图。因此错误。尝试更改插座名称。在我的情况下它工作得很好。

回答by Leonif

I found on chinese forum that it could be happen when you have similar names for ViewController and view from XIB.

我在中文论坛上发现,当您的 ViewController 和来自 XIB 的视图具有相似的名称时,可能会发生这种情况。

Example SpecialOfferViewControllerand SpecialOfferView

示例SpecialOfferViewControllerSpecialOfferView

I renamed

我改名了

SpecialOfferViewControllerand OfferViewand error has gone!!!

SpecialOfferViewControllerOfferView并且错误消失了!!!

回答by Nathan Hosselton

I had a xib file named FooViewand a view controller class named FooViewController.

我有一个名为 xib 的文件FooView和一个名为FooViewController.

FooVewwas not intended to the be the root view of FooViewController, only a subview. Yet, UIViewController's default behavior is to automatically attempt exactly that. And thus, my app would crash with this error at the first point that my controller's viewwas accessed.

FooVew不打算成为 的根视图FooViewController,只是一个子视图。然而, UIViewController 的默认行为是自动尝试完全相同的. 因此,我的应用程序会在第一次view访问我的控制器时出现此错误。

This behavior can be avoided using one of:

可以使用以下方法之一避免这种行为:

  1. Rename your controller class or the xib file to dissociate them
  2. override var nibName: String? { nil }
  3. override func loadView()and assign to viewyourself
  1. 重命名您的控制器类或 xib 文件以分离它们
  2. override var nibName: String? { nil }
  3. override func loadView()并分配给view自己

I went with 2 as it seemed the least likely sneak back to bite me later.

我选择了 2,因为它似乎最不可能在以后偷偷回来咬我。