ios - 如何停止跳转到下一个 View Controller ? ( swift )

标签 ios swift swift3 segue uistoryboardsegue

我正在使用 segue 将数据从一个 viewController 发送到 destinationViewController。我设置按钮并使用 push segue 连接到下一个 View Controller 。在第一个 View 中,我设置了一个 bool 值来检查图像是否为空。如果有空的,它会弹出警报提醒用户。如果成功,它会将图像发送到下一个 View Controller 。

虽然我可以传输图像并弹出警报,但如果有任何空白,它也会跳转到下一个 View Controller 。此外,警报是在跳转到下一个 View Controller 后弹出的,而不是在第一个 View Controller 中弹出

现在,这是代码

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

    if Jumpsuit.isHidden == false && self.Hair.image != nil && self.Jumpsuit.image != nil && self.Shoes.image != nil {

        let DestViewController : ChooseBGViewController = segue.destination as! ChooseBGViewController

        DestViewController.imgHair = Hair.image
        DestViewController.imgJumpsuit = Jumpsuit.image
        DestViewController.imgShoes = Shoes.image
        DestViewController.dressingtype = 1
    } else if Jumpsuit.isHidden == false && self.Hair.image == nil || self.Jumpsuit.image == nil || self.Shoes.image == nil {
        let alert = UIAlertView()
        alert.title = "No Image"
        alert.message = "Please check again"
        alert.addButton(withTitle: "OK")
        alert.show()

    }
}

如何停止跳转到下一个 View Controller 并在第一个 View Controller 中弹出警报。请帮忙!

最佳答案

您可以使用 shouldPerformSegue(withIdentifier:sender:)为此,并根据您的条件返回 Bool 值。

override func shouldPerformSegue(withIdentifier identifier: String, sender: Any?) -> Bool {
    if Jumpsuit.isHidden == false && self.Hair.image == nil || self.Jumpsuit.image == nil || self.Shoes.image == nil {
         //Show UIAlertController instead of UIAlertView it is deprecated
         return false
    }
    return true
}

关于ios - 如何停止跳转到下一个 View Controller ? ( swift ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41504725/

相关文章:

iphone - 我可以从 iOS 模拟器中访问 App Store 吗?

ios - 适用于 iOS 应用程序(而非 Xamarin iOS)的 Azure 应用服务库可与 Azure 推送通知中心配合使用,现在移动应用服务已弃用

ios - Alamofire 缓存问题

swift - 升级我的应用程序以包含预填充的数据

ios - 两个 UIBezierPaths 交集作为一个 UIBezierPath

ios - 如何让多个用户同时更新我的​​一个 CloudKit 记录而不重叠?

swift - swift 中的按位和算术运算

ios - 如何在 CollectionViewCell 中设置元素的框架?

ios - 在 Swift 2 中使用 Parse 检索数据

ios - 如何以编程方式设置 UITableView header 的约束以同时支持 RTL 和 LTR?