swift - "Application tried to present modally an active controller"iOS8 - 当我想在注销时显示 AlertView 时

标签 swift ios8 swift2 alertview

嗯,就 swift 2 而言,我确实是一个初学者,并且我在 presentViewController 中遇到错误,这只是注销之前的警报 View 。但我收到一个尝试以模态方式呈现事件 Controller 的应用程序。好吧,我有一个菜单栏,使用 SWRevealViewControllerMenuBarTableViewController 类来保存菜单栏。 Logout 按钮位于菜单栏的最后一个选项上,我正在浏览每个菜单,它工作正常,但这个 Logout AlertView 使其崩溃并收到该错误。这是代码。

class MenuBarTableViewController: UITableViewController {
    private struct PMText {
       static let logoutBody = "Are you sure you want to log out?"
    }

    var alert = UIAlertController(title: "Logout", message: PMText.logoutBody, preferredStyle: UIAlertControllerStyle.Alert)

    override func viewDidLoad() {
        super.viewDidLoad()

        alert.addAction(UIAlertAction(
             title: "No",
             style: UIAlertActionStyle.Cancel)
        { (action: UIAlertAction) -> Void in
             //do nothing just to close the alertView
            }
        )

        alert.addAction(UIAlertAction(
             title: "Yes",
             style: UIAlertActionStyle.Default)
        { (action: UIAlertAction) -> Void in
             self.performSegueWithIdentifier("log out", sender: nil)
            }
        )

        tableView.separatorColor = UIColor.clearColor()
    }

    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
         for index in 0..<tableView.numberOfRowsInSection(indexPath.section) {
         let cellIndexPath = NSIndexPath(forRow: index, inSection: indexPath.section)
         let cell = tableView.cellForRowAtIndexPath(cellIndexPath)!
         if index == indexPath.row {
            cell.backgroundColor = UIColor(red: 254/255, green: 255/255, blue: 197/255, alpha: 1)
         } else {
             cell.backgroundColor = UIColor.whiteColor()
         }


         if (indexPath.row == 6) {
             presentViewController(alert, animated: true, completion: nil)
         }
       }
    }



}

嗯,我不确定是什么导致我的代码崩溃并收到这种异常,因为我在其他 View Controller 中多次执行警报 View Controller 。但这是我第一次在 MenuBarTableViewController

中尝试这个

最佳答案

我找到了一个解决方案,通过使用 SWReveal 为菜单栏创建一个简单的 swift 文件,而不是创建一个 cocoa touch 文件。因此,它现在将导入 Foundation,而不是导入 UIKit。菜单栏 slider 遇到了问题,因为当 slider 是 cocoa touch 时, slider 被视为 Controller ,而 AlertView 是要呈现的另一个 View Controller 。

关于swift - "Application tried to present modally an active controller"iOS8 - 当我想在注销时显示 AlertView 时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39715806/

相关文章:

class - 我怎样才能调用这个函数?

ios - 格式化的 NSDate 输出 - 当前日期

ios - 将表格 View 文本字段保存到自定义对象数组

ios - Swift - 'sharedApplication is unavailable.' 在适当的地方使用基于 View Controller 的解决方案

swift - 使用核心数据进行本地化

objective-c - NSURLRequest setAllowsAnyHTTPSCertificate 到 Swift

ios - 如何在 Swift 中创建像指南针一样的 'sliding view' ?

ios - 如何从 iTunes Connect 中删除未经批准的应用程序?

ios - 为什么我不能在 xcode 6.1 中使用 show segue?

swift - Swift 中带有 Realm 的主键新对象