加载笔尖但视图插座未设置 - Swift版 [英] Loaded nib but the view outlet was not set - Swift edition
问题描述
我有一个项目全部在Objective C中,除了我的视图控制器,它在Swift中。
I have a project that is all in Objective C, except for my view controller, which is in Swift.
当我运行它时,我收到错误
When I run it, i get the error
由于未捕获异常而终止应用程序
'NSInternalInconsistencyException',原因:' - [UIViewController
_loadViewFromNibNamed:bundle:]加载了...笔尖,但未设置视图插座。'
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[UIViewController _loadViewFromNibNamed:bundle:] loaded the "..." nib but the view outlet was not set.'
所以我打开了我的nib文件,看看文件的所有者,我看到该视图甚至根本不显示为插座。
对于我的旧视图控制器(目标c),视图插座确实显示出来。
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.
在我的Swift视图控制器中,我尝试从UIViewController覆盖view变量以强制它为@IBOutlet,但它抱怨view变量属于类型UIView,抱怨UIView?,抱怨UIView!。
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!.
以下是
我的AppDelegate的简化版本.H
my 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
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
}
}
我需要做什么才能显示我的视图?
What do I need to do to get my view to display?
谢谢。
(是的,这是与已加载的笔尖但视图插座未设置 - InterfaceBuilder的新功能但视图插座未显示。)
(Yes, this is a similar question to Loaded nib but the view outlet was not set - new to InterfaceBuilder but the view outlet does not show up. )
推荐答案
- 首先 - 在你的nib文件中设置自定义类
(文件所有者 - >第三个图标 - >自定义类:YourViewController) - 秒 - 文件所有者中的最后一个图标 - >将view属性链接(拖动)到界面视图
像这样初始化你的ViewController:
Init your ViewController like so:
YourViewController(nibName: "YourViewName", bundle: nil)
它会起作用。不要使用View进行任何操作。
It will work. Don't do any manipulations with View.
这篇关于加载笔尖但视图插座未设置 - Swift版的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!