<分区>
我一直在尝试将此代码从 Obj-C 转换为 Swift:If statement based on whatever the previous view controller was (几乎完全相同的问题)
换句话说,我想返回到用户来自的 View Controller,MainViewController 或 SearchTableViewController 在两个展开 segue 之间进行选择。
我的问题是:如何使用 Swift 而不是 Obj-c 编写与上面链接中相同的功能代码?
这是我目前的翻译(不工作,无法消除错误)
//Method
func backViewController() -> UIViewController {
let numberOfViewControllers = self.navigationController!.viewControllers.count
if numberOfViewControllers < 2 {
return nil
}
else {
return self.navigationController!.viewControllers[numberOfViewControllers - 2]
}
}
if self.backViewController() == MainViewController {
//Back to MainViewController
self.performSegueWithIdentifier("AuthorBackMain", sender: self)
}
else {
//Back to SearchTableViewController
self.performSegueWithIdentifier("AuthorBackSearch", sender: self)
}
编辑/进度:
我已将函数更改为 backViewController() -> UIViewController?
并消除了那里的错误。但是在 if 语句中出现了一个新的错误,关于二元运算符 '=='
"Binary operator '==' cannot be applied to operands of type 'UIViewController?' and 'MainViewController.Type'.