我在UITableViewController中有一个按钮。当我按下此按钮时,警报控制器会出现询问用户是否要进入购物车页面或保持在同一页面中,如下所示,
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在不同的类中,我使用自定义动画创建它。 我想当用户按下(转到购物车页面)按钮时,购物车页面显示为(显示,"推送")segue。 这就是我在警报控制器类中以编程方式推送视图控制器的方法。
@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)
}
推送视图控制器不能与Alert控制器一起使用,我不确定是不是因为我使用带有警报的导航,如果是这样,有没有办法可以推送视图控制器?
这是我在单独的故事板中创建的整个AlertController类,并将其作为初始视图控制器。
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
}
}
答案 0 :(得分:0)
问题在于,当您尝试推送CartViewController
时,没有导航控制器可以将其推开。
您以模态方式呈现AlertController
,然后尝试执行以下操作:
navigationController?.pushViewController(CartViewController, animated: true)
。如果你在这里放一个断点并打印出navigationController
,我希望它是零。
我建议您AlertController
委托回导航控制器内的UITableViewController
,并将tableview控制器推送到下一个屏幕。您也可以在导航视图中显示AlertController
,但这似乎没必要。