我有一个项目都在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的问题,但是视图没有出现.)