简体   繁体   English

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

[英]How to send the title from UIAlertAction to another UIViewController as title of that VC

I have an UIAlertController with 4 options. 我有一个带有4个选项的UIAlertController。 When I choose any of that options I want to send the name of that option to the next ViewController as Title of that ViewController. 当我选择任何一个选项时,我都希望将该选项的名称作为该ViewController的标题发送到下一个ViewController。

Here is my code: 这是我的代码:

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)
}

It is possible to send that parameter title from UIAlertAction to the next ViewController ? 可以将参数title从UIAlertAction发送到下一个ViewController吗?

As @Sh_Khan pointed out, to get the title you need action.title . 正如@Sh_Khan指出的那样,要获取标题,您需要action.title

My answer is an add-on, since from your question and code it's not clear that you know how to do this. 我的回答是一个附加组件,因为从您的问题和代码中还不清楚您是否知道该怎么做。

To actually send the title from one VC to another you need to override the prepareForSegue method. 要将标题从一个VC实际发送到另一个VC,您需要重写prepareForSegue方法。

Create a global variable: 创建一个全局变量:

var selectedTitle = ""

In the action handlers set: 在动作处理程序中设置:

selectedTitle = action.title

Create a new property in your destination view controller: 在目标视图控制器中创建一个新属性:

var title = ""

And override prepareForSegue: 并覆盖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")
      }
   }
}

The callBack of the alertAction returns it alertAction的callBack返回它

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

As @Sh_Khan mentioned, you can get title of action inside action handler. 如@Sh_Khan所述,您可以在动作处理程序中获取动作的标题。
Now you need to stored selected title and pass it to next controller, like that: 现在,您需要存储选定的标题并将其传递给下一个控制器,如下所示:

Save action title: 保存动作标题:

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

Pass in prepareForSegue 传递prepareForSegue

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM