我試圖在viewControllers之間傳遞值。但問題是,價值總是零。即使值存在,傳遞值也是零
在firstVC我做的:
// Create a custom view controller
let ratingVC = RatingViewController(nibName: "RatingView", bundle: nil)
// Create the dialog
let popup = PopupDialog(viewController: ratingVC, buttonAlignment: .horizontal, transitionStyle: .bounceDown, gestureDismissal: true)
ratingVC.selectedTestString = "HELLO"
// Present dialog
self.present(popup, animated: true, completion: nil)
而在secondVC:
var selectedTestString: String! //Unwrapping because I know value does exist
override func viewDidLoad() {
super.viewDidLoad()
print(selectedTestString) //Gives nil
}
我與.xib
做第一次,但我認爲這個概念應該是一樣的。
也許在obj-c中存在類似弱和強的東西 – SeanChense
你可以嘗試從'let'使'ratingVC'變成'var'。它可能必須做一些'ratingVC'副本,而不是一個引用(函數式編程性質的swift)。或者在創建'popup'前更好地設置該值 – MjZac