2017-10-08 65 views
-1

当我试图在XCode 9上使用Swift 4从一个视图切换到另一个视图时,它给了我这个错误。似乎无法通过UIButton切换到另一个故事板

的libC++ abi.dylib:与 类型NSException

的未捕获的异常终止我使用一个按钮,切换从MainLandingViewMain是默认视图(main.storyboard),而LandingViewmain.storyboard中的另一个视图控制器。

下面是我用来使这个工作的代码。

let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil) 
let newViewController = storyBoard.instantiateViewController(withIdentifier: "LandingView") 
self.present(newViewController, animated: true, completion: nil) 

[above]所有包含在我想要使用的按钮的IBAction中。

只是为了澄清,LandingView是一个故事板ID,我用它来从默认视图切换到LandingView。请注意,这全部包含在Main.storyboard之下。

+0

发布一些您的崩溃日志的更多信息。如果您使用的是Storyboard,请确保您为每个屏幕指定了唯一的“标识符”。 – Glenn

+0

改用segues。修复了这个问题。 – Sanjay

回答

0

您还没有指定newViewController的类型。

试试这个:

let storyBoard: UIStoryBoard = UIStoryBoard(name: "Main", bundle: nil) 
    let newViewController = storyboard.instantiateViewController(withIdentifier: "LandingView") as! younewViewControllerType 
    self.present(newViewController, animated: true, completion: nil) 

要确定你已经宣布你LandingView的ViewController子类,你可以在界面生成图像中引用的身份检查如下:

enter image description here

在自定义Class,Class属性定义了你的自定义子类。在声明你的newViewController时,你必须声明它为那种类型。

+0

这个类型是什么? – Sanjay

+0

这可能是LandingViewController。 –

+0

使用未声明的类型“UIStoryBoard”错误。你确定这是Swift 4吗? – Sanjay