Flo
Flo

Reputation: 449

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

I have an UIAlertController with 4 options. 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.

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 ?

Upvotes: 0

Views: 49

Answers (3)

lajosdeme
lajosdeme

Reputation: 2407

As @Sh_Khan pointed out, to get the title you need 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.

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:

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

Upvotes: 2

ManWithBear
ManWithBear

Reputation: 2875

As @Sh_Khan mentioned, you can get title of action inside action handler.
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

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

Upvotes: 0

Shehata Gamal
Shehata Gamal

Reputation: 100503

The callBack of the alertAction returns it

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

Upvotes: 0

Related Questions