即使值确实存在,传递值也为零

时间:2016-11-05 14:29:46

标签: ios swift

我尝试在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)

在第二个VC中:

var selectedTestString: String! //Unwrapping because I know value does exist

override func viewDidLoad() {
        super.viewDidLoad()
    print(selectedTestString) //Gives nil

    }

我第一次使用.xib,但我认为概念应该是相同的。

1 个答案:

答案 0 :(得分: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)