在我的第一个ViewController中:

var storyboard = UIStoryboard(name: "Main", bundle: nil)
var controller = storyboard.instantiateViewController(withIdentifier: "backGroundController") as! BackGroundWithContainer
delegate?.myFunction(float: floatItryToPass)

在我的第二个VC中:
let whiteView = UIView()
@IBOutlet var TopBarView: UIView!

func takeScrollUpFloat(float: CGFloat) {

    print("scrollUpFloatTaker: ", float) //works fine
    whiteView.center.y = view.frame.height //bad access error here
    TopBarView.center.y = view.frame.height //found nil error here
}

难点在于更改在第二个ViewController中定义的值。我怎么能不打电话给segue就解决这个问题呢?我知道在打开视图时有无数种方法可以做到,但我必须在再次加载第二个视图时解决它。第二个视图当前正在运行。

最佳答案

下面是解决方法

var storyboard = UIStoryboard(name: "Main", bundle: nil)
var secondController = storyboard.instantiateViewController(withIdentifier: "backGroundController") as! BackGroundWithContainer
secondController.passedFloat = floatItryToPass

现在在第二个视图控制器中
var passedFloat: CGFloat!
let whiteView = UIView()
@IBOutlet var TopBarView: UIView!

override func viewDidLoad() {

    print("scrollUpFloatTaker: ", float) //works fine
    // no bad access error here since your view is loaded, i.e viewDidLoad
    whiteView.center.y = view.frame.height
    // all your IBOutlet variables also get values by the time viewDidLoad
    // is called, so you won't be getting nil for your TopBarView
    TopBarView.center.y = view.frame.height //found nil error here
}

第一次

10-08 07:44