如何仅在从警报调用时向第二个向下钻取视图控制器添加按钮



我是iOS开发的新手,但正在取得进展。对我来说编程有点太多了。

  1. 我在第一个视图中有一个警报按钮(正常(。
  2. 点击确定,我可以调用现有的员工表视图,该视图也允许搜索员工。点击表格视图中的行将转到包含员工详细信息的详细信息视图。(此表视图和详细信息视图已作为独立的导航菜单选项存在(。
  3. 现在,我需要在详细信息视图中动态添加一个按钮以选择特定员工,但仅在从警报按钮调用时(确定(。

// Code in alert
let alertController = UIAlertController(title: "Action Taken", message: scan.stringValue, preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: "OK", style: .default, handler: {(alert: UIAlertAction!) in gotoTableView()}))
alertController.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler:nil))
present(alertController, animated: true, completion: nil)
func gotoTableView () {
    let Employee1TableViewController = self.storyboard?.instantiateViewController(withIdentifier: "viewFromScanner")
    // the code below helped add searchbar to the table view. Witout this, the tableview didn't have search capability
    self.navigationController!.pushViewController(Employee1TableViewController!, animated: true)
}

// Code in tableview that the alert calls, which in turn cals a Segue to detail screen
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    performSegue(withIdentifier: "showDetail", sender: indexPath)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    let rowSelected = (sender as! IndexPath).row
    if let destinationVC = segue.destination as? EmpViewController {
        destinationVC.empIdText = getSwiftArrayFromPlist()[rowSelected]["empid"]
        destinationVC.firstNameText = getSwiftArrayFromPlist()[rowSelected]["firstname"]
        destinationVC.lastNameText = getSwiftArrayFromPlist()[rowSelected]["lastname"]
    // code goes on further to list other employee details

您可以尝试为此设置布尔值

 var currentCaller:Bool?
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    let rowSelected = (sender as! IndexPath).row
    if let destinationVC = segue.destination as? EmpViewController {
        destinationVC.empIdText = getSwiftArrayFromPlist()[rowSelected]  
        destinationVC.calledFromAlert = self.currentCaller
     }
 }

 let Employee1TableViewController = self.storyboard?.instantiateViewController(withIdentifier: "viewFromScanner") as! Employee1TableViewController
Employee1TableViewController.calledFromAlert = true

否则默认值为假

 class Employee1TableViewController:TableViewController
 {
   var calledFromAlert:Bool
 }

相关内容

最新更新