Coder44
Coder44

Reputation: 1

Opening a new viewcontroller through navigation controller after pressing an alert action

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

Answers (1)

Zeeshan Ahmad II
Zeeshan Ahmad II

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

Related Questions