以编程方式创建的动态表视图控制器在被不同的静态表视图控制器访问时不被调用

时间:2017-10-28 05:22:17

标签: ios swift xcode uitableview

虽然我将静态表视图控制器基于故事板,但我使其访问不同的动态表视图控制器,并根据单击的选项生成不同的列表。 dynmaic Table View Controller没有实际的故事板,但它是以编程方式创建的。我的静态Table View Controller故事板连接到的唯一内容是从主页面移动到此页面以及返回的后退按钮。

Storyboards for Reference

我有两个不同的类:PollSelectorTableViewController和SubPollsTableViewController,其中第一个是静态故事板,第二个没有故事板,但是根据PollSelectorTableViewController中选择的内容创建。名称都匹配正确。我的代码 PollSelectorTableViewController是:

import UIKit 

class PollSelectorTableViewController: UITableViewController {
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    print (indexPath)
    switch indexPath.row { //depending on which row is selected, place the indicated array into the SubPollsTableViewController so that it can load a new storyboard table viewer with the indicated values
    case 0: //first row
        let setGunControl: [String] = ["Background Checks", "Gun Shows", "Back"]
        let GVC = SubPollsTableViewController(setPolls: setGunControl)
        self.present(GVC, animated: true, completion: nil)
    case 1: //second row
        let setFavorability: [String] = ["Back"]
        let GVC = SubPollsTableViewController(setPolls: setFavorability)
        self.present(GVC, animated: true, completion: nil)
    case 2: //third row
        let setImmigrationPolicy: [String] = ["Back"]
        let GVC = SubPollsTableViewController(setPolls: setImmigrationPolicy)
        self.present(GVC, animated: true, completion: nil)
    case 3: //fourth row
        let setCenterOfDiseaseControl: [String] = ["Back"]
        let GVC = SubPollsTableViewController(setPolls: setCenterOfDiseaseControl)
        self.present(GVC, animated: true, completion: nil)
    case 4: // fifth row
        let setGlobalWarming: [String] = ["Back"]
        let GVC = SubPollsTableViewController(setPolls: setGlobalWarming)
        self.present(GVC, animated: true, completion: nil)
    case 5: //sixth row
        let setBack: [String] = ["Back"]
        let GVC = SubPollsTableViewController(setPolls: setBack)
        self.present(GVC, animated: true, completion: nil)
    default:
        let setBack: [String] = ["Back"]
        let GVC = SubPollsTableViewController(setPolls: setBack)
        self.present(GVC, animated: true, completion: nil)
    }
  }
}

*我将其他案例字符串数组保持为一个“返回”值,这样我就可以专注于第一个的功能。

**我添加了一个print语句来确定是否正在调用indexPath,但似乎按钮完全不再是交互式的。

SubPollsTableViewController的代码是:

import UIKit

class SubPollsTableViewController: UITableViewController {
var polls:[String]
    init(setPolls:[String]){
        self.polls = setPolls
        super.init(style: .plain)
} //set array to array indicated in PollSelectorTableViewController

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return polls.count //number of rows in table is the number of values in the array.
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    print(indexPath) //check if indexPath is being assigned
    let cell = UITableViewCell(style: .default, reuseIdentifier: "cell")
    cell.textLabel?.text = polls[indexPath.row] //place each cell name with a specific string in the array
    return cell
}
}

PollSelectorTableViewController甚至没有注册任何点击,SubPollsTableViewController也没有出现。此外,没有调用任何打印语句。我在这做错了什么?它甚至与代码有关吗?

1 个答案:

答案 0 :(得分:0)

在启动下一个视图之前,您需要构建UIStoryBoard,

首先,设置标识符:

打开你启动StoryBoard>正确的“公用事业”>识别检查器(显示“自定义类”)>识别>故事板ID>在文本字段中给出一个名称,在此示例中为“next”。

在此示例中,下一个Storyboard是“Next.storyboard”。

的ViewController:

@IBAction func touch(_ sender: Any) {



   let next = UIStoryboard(name: "Next", bundle: nil).instantiateViewController(withIdentifier:"next")

   (next as! NextViewController).setButtonText("test")

    present(next,animated: true)

}

在当前的viewController中,在调用present之前手动设置init函数,在代码调用setButtonText之上启动按钮文本。

NextViewController

import UIKit

    class NextViewController: UIViewController {


        @IBOutlet weak var button: UITextField!

        var buttontext = "button"
        override func viewDidLoad() {
            super.viewDidLoad()

            button.text = buttontext



            // Do any additional setup after loading the view, typically from a nib.
        }

        override func viewDidAppear(_ animated: Bool) {

        }

        func setButtonText(_ buttontext:String){
         self.buttontext = buttontext
        }

        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }


    }