2016-11-05 19 views
0

我試圖在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做第一次,但我認爲這個概念應該是一樣的。

+0

也許在obj-c中存在類似弱和強的東西 – SeanChense

+1

你可以嘗試從'let'使'ratingVC'變成'var'。它可能必須做一些'ratingVC'副本,而不是一個引用(函數式編程性質的swift)。或者在創建'popup'前更好地設置該值 – MjZac

回答

4

設置值selectedTestString創建對話框之前,這將解決問題。

// Create a custom view controller 
let ratingVC = RatingViewController(nibName: "RatingView", bundle: nil) 

ratingVC.selectedTestString = "HELLO" 

// Create the dialog 
let popup = PopupDialog(viewController: ratingVC, buttonAlignment: .horizontal, transitionStyle: .bounceDown, gestureDismissal: true) 

// Present dialog 
self.present(popup, animated: true, completion: nil) 
+1

@Tarvo添加進一步說明。將'ratingVC'傳遞給'PopupDialog init'會導致調用'viewDidLoad'。所以在創建PopupDialog之後設置'selectedTestString'就太遲了。如果你已經使用了調試器並設置了合適的斷點,你會意識到這一點。 – rmaddy

+0

真的幫助了我! –

相關問題