Reputation: 1
I am trying to open a different view controller after pressing an alert action in my original view controller. How can I do this by using navigation controller?
func showAlert()
{
let alert = UIAlertController(title: "Selection", message: "Please select which
type of stats you would like to view", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Team Stats", style: .default, handler:
{
action in print("tapped team stats")
}))
alert.addAction(UIAlertAction(title: "Player Stats", style: .default, handler:
{
action in print("tapped player stats")
}))
alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler:
{
action in print("tapped dismiss")
}))
present(alert, animated: true)
}
Upvotes: 0
Views: 35
Reputation: 1193
private func showTeamStats() {
let teamVC = YourTeamViewController()
navigationController?.pushViewController(teamVC, animate:true)
}
private func showplayerStats() {
let playerVC = YourPlayerViewController()
navigationController?.pushViewController(playerVC, animate:true)
}
func showAlert()
{
let alert = UIAlertController(title: "Selection", message: "Please select which
type of stats you would like to view", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Team Stats", style: .default, handler:
{
self.showTeamStats()
}))
alert.addAction(UIAlertAction(title: "Player Stats", style: .default, handler:
{
self.showPlayerStats()
}))
alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler:
{
action in print("tapped dismiss")
alert.dismiss(animated:true)
}))
present(alert, animated: true)
}
Note: It will work only if your parent view controller embedded in navigation controller
Upvotes: 1