gpt4 book ai didi

ios - 无法从 MasterViewController 访问 DetailViewController

转载 作者:行者123 更新时间:2023-11-30 11:37:57 25 4
gpt4 key购买 nike

我正在我的应用程序中实现 SplitViewController。我的问题是,当我在较小的设备(例如 iphone5)上显示此内容时,我的 SplitViewController 的 masterViewController 显示正常,但我无法访问我的 DetailViewController。如何对分割 View Controller 进行编程,以便当单击 MasterViewController 中的单元格时,它会将我转发到 DetailViewController?

*请注意,在较大的设备(例如 iPad)上,详细信息和主视图 Controller 显示在单击 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)

}


}

最佳答案

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

关于ios - 无法从 MasterViewController 访问 DetailViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49539435/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com