我正在创建一个投票应用程序,其中有几个不同的投票可供选择。为此,我在情节提要中创建了一个名为PollSelectorTableViewController的视图控制器。看起来是这样的:
ios - 如何从不在 Storyboard 中但以编程方式创建的 View  Controller 呈现位于“主” Storyboard 中的 View  Controller ?-LMLPHP
它是一个静态表单元格,当我按下它时,它将转换为subcollstableviewcontroller。但是,这不是故事板的一部分。
这是subcollstableviewcontroller中的代码(不包括我遇到问题的地方),它工作得很好:

import UIKit

class SubPollsTableViewController: UITableViewController {
var polls:[String]
static var pollSelectOptions:[String] = [""]

let blueColor = UIColor(red: 0.0, green: 122.0/255.0, blue: 1.0, alpha: 1.0)
let redColor = UIColor(red: 1.0, green: 41.0/255.0, blue: 0.0, alpha: 1.0)

init(setPolls:[String]){
    self.polls = setPolls
    print(self.polls)
    super.init(style: .plain)
}


required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return polls.count
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    print(indexPath)
    let cell = UITableViewCell(style: .default, reuseIdentifier: "cell")
    if (indexPath.row == 0){
        cell.textLabel?.text = polls[indexPath.row]
        cell.textLabel?.font = cell.textLabel?.font.withSize(50.0)
        cell.textLabel?.textAlignment = NSTextAlignment.center
    }
    else if (indexPath.row == (polls.count - 1)){
        cell.textLabel?.text = polls[indexPath.row]
        cell.textLabel?.textColor = redColor
        cell.textLabel?.font = cell.textLabel?.font.withSize(25.0)
        cell.textLabel?.textAlignment = NSTextAlignment.center

    }
    else{
        cell.textLabel?.text = polls[indexPath.row]
        cell.textLabel?.textColor = blueColor
        cell.textLabel?.font = cell.textLabel?.font.withSize(21.0)
        cell.textLabel?.textAlignment = NSTextAlignment.center

    }
    return cell
}

*注意代码没有完成
从这里开始,我试图使当一个单元格被点击时,它变为我在故事板中定义的一个视图控制器(名为PollViewController)。以下是视图控制器的图像:
ios - 如何从不在 Storyboard 中但以编程方式创建的 View  Controller 呈现位于“主” Storyboard 中的 View  Controller ?-LMLPHP
我试着让它工作的是:
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    print (indexPath)
    if(polls[indexPath.row] == "Background Checks"){
        print("Background Checks Accessed")
        SubPollsTableViewController.pollSelectOptions = ["Increase Drastically","Increase Slightly","Stay the Same","Decrease Slightly","Decrease Drastically"]
        actioncall()
    }
    }

    func actioncall() {

    if let PVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "PollViewController") as? PollViewController {
        PVC.selectOptions = SubPollsTableViewController.pollSelectOptions
        self.present(PVC, animated: true, completion: nil)
    }
    }

这应该会导致子collstableviewcontroller更改为PollViewController,但我的异常断点会将其停止在
    self.present(PVC, animated: true, completion: nil)

不管怎样,让它运行会出现“libc++abi.dylib:以NSException类型的意外异常终止”
为什么我会犯这个错误?我该如何解决这个问题以便他们切换?顺便说一下,PollViewController中的文本只是占位符文本。我得到的代码是基于堆栈交换内外的几个不同的源代码,但它仍然不起作用。

最佳答案

显示PollViewController的代码是正确的,这类异常与outlets连接有关,因此您可以在PollViewController中验证outlets是否正确连接。

关于ios - 如何从不在 Storyboard 中但以编程方式创建的 View Controller 呈现位于“主” Storyboard 中的 View Controller ?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46994772/

10-14 21:43