放松搜索不能取消视图

时间:2018-09-10 08:51:45

标签: swift segue unwind-segue

问题:

放开segue不会消除视图,即使它起作用。

场景:

我有一个“添加产品”屏幕,添加产品时,我启动放松搜索功能,依次显示一个弹出窗口:“添加了新产品”。

事实: 1)产品已成功添加到数据库 2)在目标中展开搜索的@IBAction中显示“已添加产品”弹出窗口,并显示 3)在AddProductScreen视图上显示弹出窗口“已添加产品”

图片1:解散的存在 enter image description here

@IBAction位于目标视图中:

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

    }

在“添加产品视图”中,单击“确认”后,将显示一条警告消息,“您确定吗?”然后提示您单击“是”或“否”。

这是警报的代码:

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

我在这里做错了什么?

1 个答案:

答案 0 :(得分:0)

由于没有有关Storyboard设置和代码的更多详细信息,因此很难说出确切的问题是什么。 但是,我假设您有两个视图控制器,我将尝试指导您完成unwindSegue的步骤,您可以检查自己是否错过了某些东西。

假设您有两个视图控制器:FirstViewController and AddProductViewController , and 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之前也有类似的问题。

我希望此信息可以帮助您找出问题所在。