【问题标题】:swift conditionally call unwind segue using shouldPerformSegue使用 shouldPerformSegue 快速有条件地调用 unwind segue
【发布时间】:2018-03-02 07:11:33
【问题描述】:

我有两个视图控制器:Step3VC(我们称之为“A”)和 Step3AddJobVC(我们称之为“B”)。我正在尝试验证“B”上的一些数据,然后再执行回“A”的展开搜索。

'B' 需要一些用户输入,我想验证用户输入是否重复。用户正在制作一份家务清单,因此重复的名称将不起作用。当用户点击“保存”时,展开转场执行,并将数据添加到数组中。

问题是:数组位于“A”上,但需要在“B”上进行验证,然后才能调用“A”。我该怎么做?

我的尝试:

我尝试在“B”中使用shouldPerformSegue,但数组返回空白[]。所以这不好。这是来自“B”的代码:

override func shouldPerformSegue(withIdentifier identifier: String, sender: Any?) -> Bool {
    print("identifier is: ", (identifier))
    print("sender is: ", (sender)!)
    let newVC = Step3VC()
    print(newVC.dailyJobs)
    return false
}

然后我尝试在 unwind segue 期间将验证放入“A”...

@IBAction func unwindToStep3VC(sender: UIStoryboardSegue) {
    let sourceVC = sender.source as! Step3AddJobVC
    let updatedJob = sourceVC.job

    // check for duplicate names
    for name in dailyJobs {
        print(name.name)
        if name.name.lowercased() == (sourceVC.jobTextField.text?.lowercased()) {        // check to see if lowercased text matches
            print("error")
            // call alert function from sourceVC
            sourceVC.duplicateNameCheck()
            return
        }
    }
    if let selectedIndexPathSection = jobsTableView.indexPathForSelectedRow?.section {      // if tableview cell was selected to begin with

        // Update existing job
        if selectedIndexPathSection == 0 {
            let selectedIndexPathRow = jobsTableView.indexPathForSelectedRow
            dailyJobs[(selectedIndexPathRow?.row)!] = updatedJob!
            jobsTableView.reloadData()
        } else if selectedIndexPathSection == 1 {
            let selectedIndexPathRow = jobsTableView.indexPathForSelectedRow
            weeklyJobs[(selectedIndexPathRow?.row)!] = updatedJob!
            jobsTableView.reloadData()
        }
    } else {

        // Add a new daily job in the daily jobs array
        let newIndexPath = IndexPath(row: dailyJobs.count, section: 0)
        dailyJobs.append(updatedJob!)
        jobsTableView.insertRows(at: [newIndexPath], with: .automatic)
    }
}

...但它给出了错误:

popToViewController:transition: called on <ToDo_App.SetupNavController 0x7fcfd4072c00> while an existing transition or presentation is occurring; the navigation stack will not be updated.

如果我取出“if”验证码,展开转场将正常工作。数据被传输并做正确的事情。问题是如果用户输入了重复的条目,我不知道如何阻止它们。

这是我检查用户输入是否重复的代码:

// check for duplicate names
    for name in dailyJobs {
        print(name.name)
        if name.name.lowercased() == (sourceVC.jobTextField.text?.lowercased()) {        // check to see if lowercased text matches
            print("error")
            // call alert function from sourceVC
            sourceVC.duplicateNameCheck()
            return
        }
    }

我错过了什么?有一个更好的方法吗?当我在“B”中时,如何调用“A”中的变量来执行我的验证,然后再调用/执行展开转场?

【问题讨论】:

  • 您的第一种方法是正确的,但是当您说let newVC = Step3VC() 时,您正在创建Step3VC 的新实例,因此它有一个空数组。在执行该验证时,将现有 Step3VC 的引用传递给“B”,或者在 B 和 A 之间实现委托模式,以便 B 可以询问 A 是否可以接受新值
  • 谢谢,@Paulw11!能给点代码吗?

标签: ios arrays validation swift3 unwind-segue


【解决方案1】:

您正在尝试验证 shouldPerformSegue 中正确位置的内容,您做错的事情是重新创建 Step3VC 的新对象并尝试访问从未设置值的 dailyJobs。

let newVC = Step3VC()
print(newVC.dailyJobs) 

您要做的是在展示 VC B 时将 dailyJobs 从 VC A 传递给 VC B,然后在 shouldPerformSegue 中检查数据是否重复。

您的代码必须如下所示:

class VCA: UIViewController {
  var dailyJobs = getDailyJobsFromServer()


  @IBAction segueToVCB(sender: UIButton) {
    let sb = UIStoryboard(name: "Main", bundle: nil)
    let vcB = sb.instantiateViewController(withIdentifier: "VCB") as! VCB
    vcB.dailyJobs = dailyJobs
    self.present(vcB, animated: true, completion: nil)
  }

}

class VCB: UIViewController {
  var dailyJobs: //DataType

  override func shouldPerformSegue(withIdentifier identifier: String, sender: Any?) -> Bool {
    //Here you do comparision with dailyJobs
    if dailyJobs == userInput {

    }
    return false
  }

}

【讨论】:

  • 太棒了!我不认为将变量从 vcA 发送到 vcB。我只是想弄清楚一旦验证后如何将数据从 B 传回 A。谢谢!我仍在努力敲定代码,但您的回答是我前进所需的提示。
猜你喜欢
  • 2015-09-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多