无法从 MasterViewController 访问 DetailViewController

Posted

技术标签:

【中文标题】无法从 MasterViewController 访问 DetailViewController【英文标题】:Unable to access DetailViewController from MasterViewController 【发布时间】:2018-03-28 16:12:21 【问题描述】:

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

*注意在 iPad 等大型设备上,详细信息和主视图控制器显示在一侧,当单击 masterVC 单元格时,我的 detailVC 会显示正确的信息。

//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】:

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

【讨论】:

以上是关于无法从 MasterViewController 访问 DetailViewController的主要内容,如果未能解决你的问题,请参考以下文章

委托和协议无法正常工作

如何将整数从 MasterViewController 传递给 DetailViewController? [复制]

从 MasterViewController 呈现的 ModalView 在 iOS 5.1 中不再是全屏

无法将值传递回 parentViewController

如何在 masterViewController 中呈现 modalViewController?

在 UISplitViewNavigator 的 MasterViewController 上呈现 ViewController