如何将标题从UIAlertAction发送到另一个VC的UIViewController

时间:2019-03-27 09:55:50

标签: ios swift

我有一个带有4个选项的UIAlertController。选择任何一个选项时,我都希望将该选项的名称作为该ViewController的标题发送到下一个ViewController。

这是我的代码:

func showOptions(){

    let actionSheet = UIAlertController(
        title: "Select a type of inspection",
        message: nil,
        preferredStyle: .actionSheet
    )
    let cancel = UIAlertAction(
        title: "Cancel",
        style: .cancel,
        handler: nil
    )

    let copyOfVehicleShortCheck = UIAlertAction(
        title: "Copy of Vehicle Short Check",
        style: .default
    ) { action in
        self.performSegue(
            withIdentifier: Constants.checklistInspectionIdentifier,
            sender: self
        )
    }

    let fullDailyDefect = UIAlertAction(
        title: "Full Daily Defect and Damage Check",
        style: .default
    ) { action in
        self.performSegue(
            withIdentifier: Constants.checklistInspectionIdentifier,
            sender: self
        )
    }

    let licenceCheck = UIAlertAction(
        title: "Licence Check Y/N",
        style: .default
    ) { action in
        self.performSegue(
            withIdentifier: Constants.checklistInspectionIdentifier,
            sender: self
        )
    }

    let vehicleShortCheck = UIAlertAction(
        title: "Vehicle Short Check",
        style: .default
    ) { action in
        self.performSegue(
            withIdentifier: Constants.checklistInspectionIdentifier,
            sender: self
        )
    }

    actionSheet.addAction(copyOfVehicleShortCheck)
    actionSheet.addAction(fullDailyDefect)
    actionSheet.addAction(licenceCheck)
    actionSheet.addAction(vehicleShortCheck)
    actionSheet.addAction(cancel)

    self.present(actionSheet, animated: true, completion: nil)
}

可以将参数title从UIAlertAction发送到下一个ViewController吗?

3 个答案:

答案 0 :(得分:1)

正如@Sh_Khan指出的那样,要获得标题,您需要action.title

我的答案是一个附加组件,因为从您的问题和代码中还不清楚您是否知道该怎么做。

要将标题从一个VC实际发送到另一个VC,您需要重写prepareForSegue方法。

创建一个全局变量:

var selectedTitle = ""

在动作处理程序集中:

selectedTitle = action.title

在目标视图控制器中创建一个新属性:

var title = ""

并覆盖prepareForSegue:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
   if segue.identifier == "insert your identifier here" {
      if let vc = segue.destination as? YourDestinationViewController {
          vc.title = selectedTitle
          print("Title set in destination VC")
      }
   }
}

答案 1 :(得分:0)

alertAction的callBack返回

let copyOfVehicleShortCheck = UIAlertAction(title: "Copy of Vehicle Short Check", style: .default) { action in
     print(action.title)
}

答案 2 :(得分:0)

就像@Sh_Khan提到的那样,您可以在动作处理程序中获取动作的标题。
现在,您需要存储选定的标题并将其传递给下一个控制器,就像这样:

保存动作标题:

let copyOfVehicleShortCheck = UIAlertAction(
    title: "Copy of Vehicle Short Check",
    style: .default
) { action in
    self.selectedActionTitle = action.title
    self.performSegue(
        withIdentifier: Constants.checklistInspectionIdentifier,
        sender: self
    )
}

传入prepareForSegue

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    guard let destination = segue.destination as? MyViewController {
        destination.title = selectedActionTitle
    }
}