2016-11-05 4 views
0

viewController間で値を渡そうとしました。しかし問題は、値が常にゼロであることです。 firstVCで値が渡されても値を渡すとnilを返す

私が行います

// 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

'rating 'を' let'から 'var'にしてみることができますか?それは参照ではない(rating of Functional Programming nature of swift) 'ratingVC'のコピーを使って何かをしなければならないかもしれません。 '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

本当に助けられました! –

関連する問題