"应用程序尝试以模态方式呈现一个活动控制器" iOS8 - 当我想在注销时呈现AlertView

时间:2016-09-27 04:09:00

标签: swift ios8 swift2 alertview

嗯,我真的是swift 2的初学者,我在presentViewController中遇到错误,这只是一个警告视图,然后才能退出。但我收到一个试图以模态方式呈现活动控制器的应用程序。好吧,我有一个菜单栏,使用包含菜单栏的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中多次执行Alert View Controller。但这是我第一次在MenuBarTableViewController

中尝试这一点

1 个答案:

答案 0 :(得分:0)

我通过使用SWReveal为菜单栏创建一个普通的swift文件,而不是创建一个可可触摸文件,找到了解决方案。现在它将import Foundation而不是import UIKit。菜单栏滑块出现问题,因为滑块在可可触摸时被视为控制器,而AlertView是另一个要呈现的视图控制器。