gpt4 book ai didi

swift - 在没有 UINavigationController 的情况下使用 Alert 推送 UIViewController

转载 作者:行者123 更新时间:2023-11-28 07:49:09 25 4
gpt4 key购买 nike

我在 UITableViewController 中有一个按钮。当我按下这个按钮时,警报 Controller 出现询问用户是想转到购物车页面还是留在同一页面,如下所示,

 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。这就是我在警报 Controller 类中以编程方式推送 View Controller 的方式。

@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)
}

推送 View Controller 无法与 Alert Controller 一起使用,我不确定是否是因为我将导航与警报一起使用,如果是,是否有办法推送 View Controller ?

这是我在单独的 Storyboard中创建的整个 AlertController 类,并将其作为初始 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
}
}

最佳答案

问题是,当您尝试推送 CartViewController 时,没有导航 Controller 可以推送它。

您以模态方式呈现 AlertController,然后尝试执行以下操作:navigationController?.pushViewController(CartViewController, animated: true)。如果你在这里放置一个断点并打印出 navigationController,我预计它是 nil。

我建议您将 AlertController 委托(delegate)回位于导航 Controller 内的 UITableViewController,并让 tableview Controller 推送到下一个屏幕。您还可以在导航 View 中显示 AlertController,但这似乎没有必要。

关于swift - 在没有 UINavigationController 的情况下使用 Alert 推送 UIViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50139237/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com