1

I have a button in UITableViewController. when I press this button, alert controller appears asks if the user wants to go to the cart page or stay in the same page, as follows,

 if user?.uid != nil{
        let title = "Item is added to your cart "
        let alert = AlertController.instance(title: title)
        present(alert, animated: true, completion: nil)
    } else{
       // Alert asks user to register..
    }

AlertController is in different class and I created it with custom animation. I want when user press (go to cart page) button, the cart page is displayed as if it was as (show,"push") segue. This is how I pushed the view controller programmatically in alert controller class.

@IBAction func showCartButton(_ sender: Any) {
    //---> go to cart page ...
   print("cart button pressed")
    //-----> this code is working fine with other UIviewcontrollers (however it is 'not working in UItableviewcontroller)
    let storyBoard : UIStoryboard = UIStoryboard(name: "Cart", bundle:nil)
    let CartViewController = storyBoard.instantiateViewController(withIdentifier: "CartPage")
    navigationController?.pushViewController(CartViewController, animated: true)
      //-----> this code shows the cart page without the navigation and tabs.. 
  //        let storyBoard : UIStoryboard = UIStoryboard(name: "Cart", bundle:nil)
 //        let CartViewController = storyBoard.instantiateViewController(withIdentifier: "CartPage")
 //        self.present(CartViewController, animated:true, completion:nil)
} 

The push view controller is not working with Alert controller, I am not sure if it is because I used the navigation with the alert, and if so, is there a way I can push the view controller??

This is the whole AlertController Class I created in seperate storyboard and put it as initial view controller..

 import UIKit

 class AlertController: UIViewController{


@IBOutlet weak fileprivate var AddProductToCartLabel: UILabel!
@IBOutlet weak fileprivate var cartButton: UIButton!
@IBOutlet weak fileprivate var shoppingButton: UIButton!
@IBOutlet weak fileprivate var container: UIView!
var text = String()

override func viewDidLoad() {

    setContainer()
    setCartButton()
    setShoppingButton()


}

override func viewDidLayoutSubviews() {
    layoutContainer()
}

@IBAction func cancel(_ sender: Any) {
    dismiss(animated: true, completion: nil)
}

@IBAction func showCartButton(_ sender: Any) {
    //---> go to cart page ...
   print("cart button pressed")

    let storyBoard : UIStoryboard = UIStoryboard(name: "Cart", bundle:nil)
    let CartViewController = storyBoard.instantiateViewController(withIdentifier: "CartPage")
    navigationController?.pushViewController(CartViewController, animated: true)

     //        let storyBoard : UIStoryboard = UIStoryboard(name: "Cart", bundle:nil)
    //        let CartViewController = storyBoard.instantiateViewController(withIdentifier: "CartPage")
    //        self.present(CartViewController, animated:true, completion:nil)
}

  @IBAction func completeShopButton(_ sender: Any) {
    dismiss(animated: true, completion: nil)
  }
 }

fileprivate extension  AlertController{
// to set the view above
func setContainer() {
    let shape = CAShapeLayer()
    shape.fillColor = UIColor.white.cgColor
    container.backgroundColor = UIColor.clear
    container.layer.insertSublayer(shape, at: 0)
 }
func setCartButton(){
    cartButton.clipsToBounds = true
    //cartButton.layer.cornerRadius = 10
}
func setShoppingButton(){
    shoppingButton.clipsToBounds = true
    shoppingButton.layer.borderColor = UIColor.darkGray.cgColor
    shoppingButton.layer.borderWidth = 1
    //shoppingButton.layer.cornerRadius = 10
  }

 }
  fileprivate extension  AlertController{
  // design the size of the container
  func layoutContainer() {
   let path = UIBezierPath(roundedRect: container.bounds, cornerRadius: 10)
   let layer = container.layer.sublayers?.first as! CAShapeLayer
   layer.path = path.cgPath
 }
}
  extension AlertController{
  static func instance(title: String?) -> AlertController{
      let storyboard = UIStoryboard(name: String(describing: self), bundle: Bundle(for: self))
      let controller = storyboard.instantiateInitialViewController() as! AlertController
      controller.text = title!
      return controller
  }
 }
Amal Nasir
  • 164
  • 15

1 Answers1

0

The problem is that when you try to push CartViewController, there is no navigation controller to push it on.

You present AlertController modally, and then try to do: navigationController?.pushViewController(CartViewController, animated: true). If you put a breakpoint here and print out navigationController, I expect it is nil.

I suggest you have AlertController delegate back to your UITableViewController that is inside a navigation controller, and have the tableview controller push to the next screen. You could also present AlertController inside of a navigation view, but this seems unnecessary.

Jake G
  • 1,185
  • 9
  • 19
  • AlertController in different storyboard, I don't know how to present it modally. Since the alert controller class of UIViewController, how can I set up the delegate? – Amal Nasir May 03 '18 at 17:32