简体   繁体   English

Swift 2.0在TableView上呈现模式视图

[英]Swift 2.0 Presenting modal view over tableview

I have a tableview where I tap on disclosure button (i) to show another view modally. 我有一个表格视图,在其中点击披露按钮(i)以模态显示另一个视图。 Every time the modal view appears the app crashes with this error; 每次出现模式视图时,应用都会因该错误而崩溃;

NSInvalidArgumentException', reason: 'Application tried to present modally an active controller NSInvalidArgumentException',原因:“应用程序尝试以模态形式显示活动控制器

This is being called on the ExamTableView; 这在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)
    }

It's called from the tap of the button in the ExamTableView; 它是通过点击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)
}

And I do get the new view's viewDidLoad as I get the print of the htmlFile variable. 当我得到htmlFile变量的输出时,我确实得到了新视图的viewDidLoad。

    override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
    print(htmlFile)
}

But as soon as the modal view lands of the screen, the app crashes. 但是,一旦模式视图进入屏幕,该应用程序便崩溃了。 I can see it arriving, but then it's gone. 我可以看到它到达,但是随后消失了。 Needless to say, I'm new to Swift and of course Swift 2. 不用说,我是Swift的新手,当然还有Swift 2。

Anyone out there that can see what I'm doing wrong? 有人可以看到我在做什么错吗?

You are not supposed to call presentViewController in prepareForSegue, simply remove the line self .performSegueWithIdentifier("showHTML", sender: self) and it will work - assuming you set up the segue correctly in the storyboard 您不应该在prepareForSegue中调用presentViewController,只需删除self .performSegueWithIdentifier("showHTML", sender: self) ,它将起作用-假设您在情节提要中正确设置了segue

In prepare for segue you should prepare the destination viewcontroller by passing over relevant parameters. 在准备segue时,您应该通过传递相关参数来准备目标视图控制器。 PrepareForSegue is called before the transition happens - assuming shouldPerformSegue returns true. 在过渡发生之前会调用PrepareForSegue假设shouldPerformSegue返回true。 presentViewController is already called implicitly after prepareForSegue is finished, triggering it manually will cause the error you stumbled upon. prepareForSegue完成之后,已经隐式调用了presentViewController ,手动触发它会导致您偶然发现的错误。

Hope i could help 希望我能帮上忙

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM