我有一个表格 View ,我点击披露按钮 (i) 以模态显示另一个 View 。每次出现模态视图时,应用程序都会因此错误而崩溃;
NSInvalidArgumentException', reason: 'Application tried to present modally an active controller
这是在 ExamTableView 上调用的;
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "showHTML" {
let vc : HTMLViewController = segue.destinationViewController as! HTMLViewController
vc.htmlFile = htmlFile
presentViewController(vc, animated: true, completion: nil)
}
它是通过点击 ExamTableView 中的按钮调用的;
override func tableView(tableView: UITableView, accessoryButtonTappedForRowWithIndexPath indexPath: NSIndexPath) {
let examListing = exams[indexPath.row]
htmlFile = examListing.htmlFile!
print("File: " + htmlFile)
self .performSegueWithIdentifier("showHTML", sender: self)
}
当我得到 htmlFile 变量的打印时,我确实得到了新 View 的 viewDidLoad。
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
print(htmlFile)
}
但是一旦模态视图出现在屏幕上,应用程序就会崩溃。我可以看到它到达,但随后它就消失了。 不用说,我是 Swift 的新手,当然也是 Swift 2 的新手。
有人能看出我做错了什么吗?
最佳答案
您不应该在 prepareForSegue 中调用 presentViewController,只需删除该行
self .performSegueWithIdentifier(“showHTML”,发件人: self )
它会起作用——假设你在 Storyboard中正确设置了 segue
在准备 segue 时,您应该通过传递相关参数来准备目标 View Controller 。 PrepareForSegue
在转换发生之前调用 - 假设 shouldPerformSegue
返回 true。
presentViewController
已在 prepareForSegue
完成后隐式调用,手动触发它会导致您偶然发现的错误。
希望能帮到你
关于ios - Swift 2.0 在 tableview 上呈现模态视图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32184105/