将数据传回导航控制器Swift 3

时间:2017-06-21 18:37:32

标签: ios iphone swift3 uinavigationcontroller xcode8

我对Xcode和Swift很新,我试图从前一页传回数据。到目前为止,我所做的工作,但它利用我创建的segue来显示上一页。除了它在导航视图控制器中之外,这不会是一个问题。因此,当您单击保存按钮时,它会将数据传递给我的选择视图控制器,但如果您单击页面上的后退按钮,则会返回编辑页面。我希望它在导航窗格中返回,这样当您单击后页时,它会将其转到查看我的图表的页面。所以我不认为使用segue这样做是正确的选择,因为我通常会向前传递数据。我想将数据传递回上一个导航视图控制器。您将看到我的取消按钮执行此操作。

Story Board

修改ViewController

  class EditUserViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource, UITextFieldDelegate {

     var officeSelected = ""
     var utilSelected = ""

     @IBOutlet weak var officePicker: UIPickerView!

    @IBOutlet weak var slider: UISlider!
    @IBAction func UtilSlider(_ sender: UISlider) {
        slider.value = roundf(slider.value)
        utilSelected = String(Int(sender.value))
    }

    let offices = ["NYC", "LA", "Boston"]

    func numberOfComponents(in pickerView: UIPickerView) -> Int
    {
        return 1
    }

    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int
    {
        return offices.count
    }

    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String?
    {
        return offices[row]
    }


    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)
    {
        officeSelected = offices[row] 

    }


    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    @IBAction func CancelEdit(_ sender: UIBarButtonItem) {
        self.dismiss(animated: true, completion: nil)
    }


    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        let choicesView = segue.destination as! ChoicesViewController
        choicesView.officeText = officeSelected
        choicesView.utilText = utilSelected
    }
}

选择ViewController

    class ChoicesViewController: UIViewController{

     @IBOutlet weak var officeLabel: UILabel!

     @IBOutlet weak var AvalLabel: UILabel!

     var utilText = String()
     var officeText = String()

    override func viewDidLoad() {
        super.viewDidLoad()

        officeLabel.text = officeText
        AvalLabel.text = utilText

        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

0 个答案:

没有答案