无法从MasterViewController访问DetailViewController

时间:2018-03-28 16:12:22

标签: ios swift segue uisplitviewcontroller

我正在我的应用程序中实现SplitViewController。我的问题是,当我在较小的设备上显示它时,如iphone5,我的SplitViewController的masterViewController显示正常,但我无法访问我的DetailViewController。如何编程我的分割视图控制器,以便在单击MasterViewController中的单元格时将其转发给DetailViewController?

*请注意较大的设备,例如iPad,其中详细信息和主视图控制器显示在我的detailVC上,当单击masterVC单元时显示正确的信息。

//This is the class for my split view controller.

class SplitViewController: UISplitViewController, UISplitViewControllerDelegate {

override func viewDidLoad() {
    super.viewDidLoad()
self.delegate = self
    self.preferredDisplayMode = .allVisible
    // Do any additional setup after loading the view.
}

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

func splitViewController(_ splitViewController: UISplitViewController, collapseSecondary secondaryViewController: UIViewController, onto primaryViewController: UIViewController) -> Bool {
    return true
}


}

以下是我的detailViewController的代码

class Detail: UITableViewController {

var word: Word? {
    didSet (newWord) {
        self.tableView.reloadData()
    }
}


override func viewDidLoad() {

    super.viewDidLoad()

}

override func didReceiveMemoryWarning() {

    super.didReceiveMemoryWarning()
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return word?.words.count ?? 0
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "DetailCell", for: indexPath)
    cell.textLabel?.text = self.word?.words[indexPath.row] ?? ""

    return cell
}
func splitViewController(_ splitViewController: UISplitViewController, collapseSecondary secondaryViewController:UIViewController, onto primaryViewController:UIViewController) -> Bool {
    return true
}

}
extension Detail: WordSelectionDelegate {
func wordSelected(newWord: Word) {
    word = newWord
}

}

以下是我的masterViewController的代码

protocol WordSelectionDelegate: class {
func wordSelected(newWord: Word)
}

class MasterViewController: UITableViewController{
var words = [Word]()


weak var delegate: WordSelectionDelegate?


@IBAction func homeButtonTapped(_ sender: Any) {
    dismiss(animated: true) { _ in }
}




override func viewDidLoad() {
    super.viewDidLoad()



}
required init(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)!

    self.words.append(Word(name: "initial /l/ 1 syllable", words: ["lake", "lamb", "lamp", "lark", "leaf", "leash", "left", "leg", "lime", "lion", "lips", "list", "lock", "log", "look", "love", "lunch"]))

    self.words.append(Word(name: "initial /l/ multisyllabic", words: ["ladder", "ladybug", "laughing", "lawnmower", "lemon", "leopard", "leprechaun", "letters", "licking", "lifesaver", "lifting", "lightbulb", "lightning", "listen", "llama"]))

    self.words.append(Word(name: "intersyllabic /l/", words: ["alligator", "balance", "ballerina", "balloon", "bowling", "cello", "colors", "curlyhair", "dollar", "dolphin", "elephant", "eyelashes", "gasoline","goalie", "hula", "jellyfish", "olive", "pillow", "pilot", "polarbear", "rollerskate", "ruler", "silly", "telephone", "television", "tulip", "umbrella", "valentine","violin", "xylophone", "yellow"])) //xylophone", "yellow"

    self.words.append(Word(name: "final /l/", words: ["apple", "ball", "bell", "bubble", "castle", "fall", "fishbowl", "girl", "owl", "pail", "peel", "pool", "smile", "whale", "wheel"])) // "whale", "wheel"

    self.words.append(Word(name: "initial /pl/", words: ["planet", "plank", "plant", "plate", "play", "plum", "plumber", "plus"]))

    self.words.append(Word(name: "initial /bl/", words: ["black", "blanket", "blender", "blocks", "blond", "blood", "blow", "blue"]))

    self.words.append(Word(name: "initial /fl/", words: ["flag", "flipflop", "float", "floor", "flower", "fluffy", "flute", "fly"]))

    self.words.append(Word(name: "initial /gl/", words: ["glacier", "glad", "glasses", "glide", "glitter", "globe", "glove", "glue"]))

    self.words.append(Word(name: "initial /kl/", words: ["clam", "clamp", "clap", "claw", "clean", "climb", "clip", "cloud"]))

    self.words.append(Word(name: "initial /sl/", words: ["sled", "sleep", "sleeves", "slice", "slide", "slime", "slip", "slow"]))

    self.words.append(Word(name: "final /l/ clusters", words: ["belt", "cold", "elf", "golf", "melt", "milk", "shelf"]))



}


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

// MARK: - Table view data source

override func numberOfSections(in tableView: UITableView) -> Int {
    // #warning Incomplete implementation, return the number of sections
    return 1
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete implementation, return the number of rows
    return self.words.count
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! SettingsCell

    // Configure the cell...
    let word = self.words[indexPath.row]
    cell.wordGroupSwitch.isOn = isSwitchOn(at: indexPath.row)
    cell.textLabel?.text = word.name
    cell.textLabel?.backgroundColor = UIColor.clear
    cell.switchChanged = { [weak self] isOn in
        self?.wordGroup(at: indexPath.row, changedTo: isOn)
    }
    return cell
}



func wordGroup(at index: Int, changedTo value: Bool) {
    let numberOfGroupsTurnedOn = Array(0..<11).map {
        isSwitchOn(at: $0)
        }.filter { $0 }.count
    if numberOfGroupsTurnedOn <= 1 && value == false {
        //display a message

        let alertController = UIAlertController(title: "Error", message: "You cant turn off all wordgroups", preferredStyle: .alert)
        let cancelAction = UIAlertAction(title: "Dismiss", style: .cancel) { [weak self]  _ in
            self?.tableView.reloadData()
        }
        alertController.addAction(cancelAction)
        present(alertController, animated: true) { _ in }

    } else {
        // persist the setting
        UserDefaults.standard.set(value, forKey: "\(index)")
        UserDefaults.standard.synchronize()


    }

}

func isSwitchOn(at index: Int) -> Bool {
   return UserDefaults.standard.value(forKey: "\(index)") as? Bool ?? true
}

override  func tableView(_ tableView: UITableView, didSelectRowAt
    indexPath: IndexPath) {
    let selectedwords = self.words[indexPath.row]
    guard let detailVC = splitViewController?.viewControllers.last as? Detail else { return }
    detailVC.wordSelected(newWord: selectedwords)

}


}

1 个答案:

答案 0 :(得分:0)

对于最基本的解决方案,您需要实现(或调用默认情况下创建的那个,如果您在故事板中添加了拆分)showDetail segue。此segue应从主VC(或其链中的某个位置)运行到详细导航控制器。