ios – 加载笔尖,但视图插座未设置 – Swift版

前端之家收集整理的这篇文章主要介绍了ios – 加载笔尖,但视图插座未设置 – Swift版前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个项目都在Objective C,除了我的视图控制器,这是在 Swift.

当我运行它,我得到错误

Terminating app due to uncaught exception
‘NSInternalInconsistencyException’,reason: ‘-[UIViewController
_loadViewFromNibNamed:bundle:] loaded the “…” nib but the view outlet was not set.’

所以我打开了我的nib文件,看看“文件的所有者”,我看到这个视图根本不显示出来.
对于我的旧视图控制器(目标c),视图插座确实显示.

在我的Swift视图控制器中,我试图从UIViewController中重写“view”变量,以强制它是一个@IBOutlet,但是它抱怨UIView类型的“view”变量,UIView?抱怨,并抱怨UIView !

这是简化版本

我的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

#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

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

我需要做什么来让我的视图显示

谢谢.

(是的,这是一个类似于Loaded nib but the view outlet was not set – new to InterfaceBuilder的问题,但是视图没有出现.)

解决方法

>首先在你的nib文件中设置自定义
(文件所有者 – >第三个图标 – >自定义类:YourViewController)
>第二 – 文件所有者中的最后一个图标 – >链接(拖动)“视图”属性到接口视图

像你这样启动你的ViewController:

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

它会奏效不要对View进行任何操作.

猜你在找的iOS相关文章