问题:
放松segue不要忽视视图,即使它有效。
场景:
我有一个“添加产品”屏幕,当添加产品时,我启动了解开segue,这反过来显示一个弹出窗口:“添加了新产品”。
事实:1)产品成功添加到数据库2)弹出“产品添加”显示在目标中的展开segue的@IBAction
中,它出现3)弹出“产品已添加”显示在AddProductScreen
视图上
@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")
}))
我在这做错了什么?
如果没有关于Storyboard设置和代码的更多详细信息,很难说出确切的问题是什么。但我会假设你有两个视图控制器,我会尽力指导你完成unwindSegue
的步骤,你可以检查一下你是否错过了什么。
假设您有两个视图控制器:FirstViewController and
AddProductViewController, and
AddProductViewController`使用push segue呈现。
在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之前有一个类似的问题。
我希望这些信息可以帮助您找出问题所在。