在performSegue之前调用prepareForSegue

时间:2017-08-01 17:50:58

标签: ios swift xcode parse-platform uistoryboardsegue

我正在尝试执行一个segue,它将许多变量传递给下一个视图,包括一个从解析数据库中检索的变量currentID。在将currentID设置为从数据库下载的currentID之后,才应调用performSegue。但是,当我运行代码时,currentID在传递给下一个视图时最终成为空字符串。

这是由Button调用的代码:

@IBAction func submitButtonPressed(_ sender: Any) {

    let point = PFGeoPoint(latitude:0.0, longitude:0.0)
    let testObject = PFObject(className: "Person")

    testObject["inputAmount"] = inputAmount
    testObject["outputAmount"] = outputAmount
    testObject["inputCurrency"] = inputCurrency
    testObject["outputCurrency"] = outputCurrency
    testObject["location"] = point

    testObject.saveInBackground { (success, error) -> Void in

        // added test for success 11th July 2016

        if success {
            print("Object has been saved.")

            self.currentID = String(describing: testObject.objectId!)
            if(self.currentID != ""){
                self.performSegue(withIdentifier: "mainToListSegue", sender: self)
            }

        } else {
            if error != nil {
                print (error)
            } else {
                print ("Error")
            }
        }
    }
}

这是prepareForSegue方法:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    let listViewController = (segue.destination as! UINavigationController).viewControllers[0] as! ListViewController
    listViewController.inputCurrency = inputCurrency
    listViewController.outputCurrency = outputCurrency
    listViewController.inputAmount = inputAmount
    listViewController.outputAmount = outputAmount
    listViewController.currentID = currentID
    listViewController.cellContent = cellContent
}

2 个答案:

答案 0 :(得分:2)

为了满足您的需求,您必须在viewcontrollers之间连接您的segue,而不是从UIButton连接到viewcontroller。

每次你需要在调用之前准备你的segue,这就是程序:

enter image description here

然后,将其命名并使用委托方法

const divs = document.querySelectorAll('div');

const switchColor = e => {
    let color = 'transparent';

    switch (e.keyCode) {
        case 82: color = 'red';     break; 
        case 89: color = 'yellow';  break; 
        case 71: color = 'green';   break; 
        case 66: color = 'blue';    break; 
    }

    divs.forEach(div => {
        div.style.backgroundColor = color;
    });
};

document.addEventListener('keydown', switchColor);

答案 1 :(得分:0)

要从一个控制器导航到另一个控制器,请从视图控制器而不是按钮连接您的segue,它将起作用。