仅当从警报调用时,如何向第二个向下钻取View Controller添加按钮?

问题描述 投票:0回答:1

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

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

// 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
swift button programmatically
1个回答
0
投票

您可以尝试为此设置一个bool

 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

否则默认为false

 class Employee1TableViewController:TableViewController
 {
   var calledFromAlert:Bool

 }
© www.soinside.com 2019 - 2024. All rights reserved.