我一直在試圖從對象 - 這個代碼轉換爲斯威夫特聲明:If statement based on whatever the previous view controller was(幾乎完全同樣的問題)如果基於以前的視圖控制器在斯威夫特
換句話說,我想回去到用戶來自的View Controller,無論是MainViewController還是SearchTableViewController都可以在兩個unwind segues之間進行選擇。
我的問題是:我如何編寫與上面的鏈接相同的功能代碼,但在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'.
變化' - > UIViewController'到' - >'的UIViewController? – matt
使用此鏈接將其轉換爲Swift https://objectivec2swift.com/#/home/converter/請注意,如果它工作 – Nik
「不工作」和「所有幫助非常感謝」不是問題。太寬泛。要求我們爲您翻譯您的代碼也不是問題。 – matt