push viewController

Hi all, I've created an action sheet and am trying to push to a viewController that I've already created and am having some troubles. I get as far as entering the function to push to a view controller but cannot work out how to add my actual viewController that it needs to push to. This code works perfectly to bring up my action sheet with the buttons options I require.


This is my current code but I'm looking for suggestions on how to make it push to a pre-made viewController:


    @IBAction func displayActionSheet(_ sender: Any) {
        let Menu = UIAlertController(title: nil, message: "Get Your Roast On", preferredStyle: .actionSheet)
       
        let phone = UIAlertAction(title: "Call Us Now", style: .default, handler: {(alert: UIAlertAction!)->Void in print("Call Now")
    })
        let social = UIAlertAction(title: "Follow Us", style: .default, handler: {(alert: UIAlertAction!)-> Void in print("Follow")
    })
        let shop = UIAlertAction(title: "Merchandise", style: .default, handler: {(alert: UIAlertAction!)-> Void in print ("Open Shop")
    })
        let cancel = UIAlertAction(title: "Cancel", style: .cancel, handler: {(alert: UIAlertAction!)in print("Cancel Button Tapped")
    })
Menu.addAction(phone)
Menu.addAction(social)
Menu.addAction(shop)
Menu.addAction(cancel)
        present(Menu, animated: true, completion: nil)
       
    }


Thanks!

Accepted Reply

Try like this (take care it is Swift 2, you may have to adapt a little, but auto completion will help):


let followAction = UIAlertAction(title: "Follow Us", style: .default) { (handler) in

print("You follow us Now")

performSegueWithIdentifier("segueForPhone", sender: self)

}

alertController.addAction(followAction)

Replies

I assume you want to push the view controller when you tap a button ?


With the present code, it just prints to the console. Right ?


You need to define the actions that will do this.

In each action for each button, use performSegue, with a segue to lead to the controller you want.

Okay, where exaclty would I add the performSegue? would it be after the menu.addAction? or is it a completely seperate piece of code? This is completely new to me so I'm not sure how it would work

You could put it in the handlers:

(This is Swift2 syntax)


handler: { (alert: UIAlertAction!) ->Void in
     print("Call Now")
        performSegueWithIdentifier("segueForPhone", sender: self)
})

or you can write a func and pass it to the handler

func handlerForPhone() {
     print("Call Now")
        performSegueWithIdentifier("segueForPhone", sender: self)
}

        let phone = UIAlertAction(title: "Call Us Now", style: .default) {
               (handler) in self.handlerForPhone() }   // syntax with closure outside parenthesis, for better lisibility


You must have defined a segue from firstController to destination in Interface Builder, with the exact name (inlcuding uppercases) segueForPhone

I've been trying with the func to pass to the handler. With both options however, I'm having trouble because I need to create the segue from the action sheet button to the viewController I want it to push to first. The action sheet was created programatically so I don't have the action sheet button to draw a segue from in the interface builder. How can I fix this or how can I create the segue programatically from the action sheet button?

just insert the performSegue inside the button action.


You can see an example here :


h ttps://www.youtube.com/watch?v=cD02mR8UpRk

Thanks for the video. I understand how it works and I've created a segue with identifier for the push but the problem I'm having it actually implementing it into the code for the button on the actionSheet.


The code that I have for the actionSheet is as follows and this is where I need to add the performSegue code.


let alertController = UIAlertController(title: nil, message: "Get Your Roast On", preferredStyle: .actionSheet)
        alertController.addAction(UIAlertAction(title: "Follow Us", style: .default, handler: nil)) //performSegue here
        alertController.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
        self.present(alertController, animated: true, completion: nil)
  
    }

Sorry if this is really repetitive for you but I'm new to Swift and I'm not grasping this at all.


Thanks

Try like this (take care it is Swift 2, you may have to adapt a little, but auto completion will help):


let followAction = UIAlertAction(title: "Follow Us", style: .default) { (handler) in

print("You follow us Now")

performSegueWithIdentifier("segueForPhone", sender: self)

}

alertController.addAction(followAction)

Thank you! I had to adapt a little as you have said for Swift 3 and it's worked. This is the code I have now used:


@IBAction func displayActionSheet(_ sender: Any) {
        let alertController = UIAlertController(title: nil, message: "Get Your Roast On", preferredStyle: .actionSheet)
            let followAction = UIAlertAction(title: "Follow Us", style: .default, handler: {(action: UIAlertAction!)->Void in self.performSegue(withIdentifier: "moveSegue", sender: self)
        })
       
        let cancel = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
       
        alertController.addAction(followAction)
        alertController.addAction(cancel)
     
        self.present(alertController, animated: true, completion: nil)
    }