gpt4 book ai didi

swift - 放松 Segue 不消除 View

转载 作者:行者123 更新时间:2023-11-30 11:10:21 25 4
gpt4 key购买 nike

问题:

放松转场而不是关闭 View ,即使它有效。

场景:

我有一个“添加产品”屏幕,当添加产品时,我会启动 unwind segue,这又会显示一个弹出窗口:“已添加新产品”。

事实:1) 产品成功添加到数据库2)目标中unwind segue的@IBAction中显示弹出“产品已添加”,并且出现3) 弹出窗口“已添加产品”显示在 AddProductScreen View

图1:Unwind segue的存在 enter image description here

@IBAction 位于目标 View 中:

@IBAction func unwindFromAddProductViewSuccess(segue: UIStoryboardSegue)
{
AlertManager.showTimedAlert(title: "Success", message: "New Product added", timeShowing: 1, callingUIViewController: self)
}

AddProductView中调用函数来注册产品:

private func registerProductAndPerformSegue(productToRegister prod: Product)
{
self.registerProduct(prodToRegister: prod)
Constants.logger.debug("New product registered")
self.performSegue(withIdentifier: "unwind_from_add_product_success", sender: self)

}

在“添加产品 View ”中,单击“确认”后,将显示一条警告“您确定吗?”然后系统会提示您单击"is"或“否”。

这是警报的代码:

AlertManager.ShowAlert(controllerToShowAlert: self, alertTitle: LocalizationStrings.Products.ADD_PRODUCT_TITLE, alertText: LocalizationStrings.Products.ADD_PRODUCT_CONFIRMATION,
alertStyle: UIAlertControllerStyle.alert,
leftButtonAction: UIAlertAction(title: LocalizationStrings.YES_TEXT, style: .default, handler:
{
(action: UIAlertAction!) in

let user = Auth.auth().currentUser

if let user = user
{
let product : Product = Product(name: name!, price: Double(price!)!, currency: "USD", description: desc!,
location: "USA", ownerID: user.uid, ownerName: user.displayName!, uniqueID: "", mainImageURL: nil, category: category)


let storageRef = Storage.storage().reference().child(product.getUniqueID()).child("pic0.jpg")
if let mainChosenImage = self.selectedImageToUpload
{
Constants.logger.debug("Product picture chosen")
if let uploadData = UIImageJPEGRepresentation(mainChosenImage, 0.2)
{
storageRef.putData(uploadData, metadata: nil)
{
(StorageMetaData, error) in
if error != nil
{
Constants.logger.error("Add Product: Couldn't store image in database!")
return
}

self.mainImageURL = StorageMetaData?.downloadURL()?.absoluteString
if let urlString = self.mainImageURL
{
product.AddImageURLToProduct(URL: urlString)
self.registerProductAndPerformSegue(productToRegister: product)
}
}
}
else
{
Constants.logger.error("Couldn't convert uploaded image to UIImageJPEGRepresentation")
}
}
else
{
Constants.logger.debug("Product picture NOT chosen")
self.registerProductAndPerformSegue(productToRegister: product)
}
}
}),

rightButtonAction: UIAlertAction(title: LocalizationStrings.NO_TEXT, style: .cancel, handler: { (action: UIAlertAction!) in
print("Handle Cancel Logic here")
}))

我在这里做错了什么?

最佳答案

如果没有有关 Storyboard设置和代码的更多详细信息,很难判断确切的问题是什么。但我假设您有两个 View Controller ,我将尝试引导您完成执行 unwindSegue 的步骤,您可以检查是否遗漏了某些内容。

假设您有两个 View Controller :FirstViewController 和AddProductViewController,并且AddProductViewController` 使用推送转场呈现。

FirstViewController 中你有:

@IBAction func unwindFromAddProductViewSuccess(segue: UIStoryboardSegue) {
AlertManager.showTimedAlert(title: "Success", message: "New Product added", timeShowing: 1, callingUIViewController: self)
}

然后,将“保存”或“添加”按钮从 AddProductViewController 连接到 unwindFromAddProductViewSuccess: 操作方法。

您还可以将“保存”或“添加”按钮连接到 SecondViewController 来调试 segue 是否被调用:

class SecondViewController: UIViewController { 
@IBOutlet weak var saveButton: UIBarButtonItem!

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
guard let uiBarButtonItem = sender as? UIBarButtonItem else {
print("There is no UIBarButtonItem sender")
return
}

if saveButton == uiBarButtonItem {
print("save button tapped")
}
print("A segue with an identifier \(segue.identifier ?? "") was called.")
}
}

假设您没有栏按钮项,并且想要手动调用 unwidnSegue,也许在用户点击 UIAlertController 操作后,那么您可以执行类似的操作这个:

@IBAction func orderButtonTapped(_ sender: UIButton) {
let alert = UIAlertController(title: "Order Placed!", message: "Thank you for your order.\nWe'll ship it to you soon!", preferredStyle: .alert)
let OKAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: {
(_)in
self.performSegue(withIdentifier: "unwindToMenu", sender: self)
})

alert.addAction(OKAction)
self.present(alert, animated: true, completion: nil)
}

在某些情况下,您尝试展开的 UIViewController 位于 UINavigationController 堆栈的底部,在这种情况下,调用 unwindSegue 将不起作用任何事物。之前有一个类似的问题,here .

希望这些信息可以帮助您解决问题。

关于swift - 放松 Segue 不消除 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52254185/

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