如何将TableC中的MVC值传递给ViewController

时间:2017-07-10 16:40:48

标签: ios swift model-view-controller

我是Swift的新手,我一直在研究一个简单的应用程序,它允许我将MVC中的信息呈现给TableView。我想要做的是 - 当在TableView中选择一行时,它将在新的ViewController中显示剩余的信息。我已经设法使用init方法从MVC调用“name”变量到原始tableView,但是我很难跟踪tableViewCells并将剩余的数据放在viewController中。

下面是代码:

// Model Variables 

import Foundation

class Close: NSObject {

var name: String
var technique: String
var example: String
var howItWorks: String

init(name: String, technique: String, example: String, howItWorks: String) {
    self.name = name
    self.technique = technique
    self.example = example
    self.howItWorks = howItWorks
} 
}

import Foundation

class CloseGroup: NSObject {

var name: String
var closesArray: [Close]

init(name: String, closesArray: [Close]) {
    self.name = name
    self.closesArray = closesArray
} 
}

这里是我保存我想要在TableView和ViewController中显示的数据的地方

class CloseDataManager: NSObject {

static func createCloseGroup() -> [CloseGroup] {

    let basicCloses = CloseGroup(name: "Basic Closes",
                                 closesArray: [
                                    Close(name: "Ask the manager",
                                          technique: "One thing we can all agree on is that people love to spend money right? Nevertheless we all have a limit, some people have no problem spending a lot while other people may not want to spend that much. This doesnt take away from the fact that regardless whatever your spending you want the best for your money, now once you've been able to create value and the prospect is interest however after looking at the price they say 'As nice as it is, I simply cant afford it' - this technique comes in to play.",
                                          example: "Look I can understand that this is a bit more then you expected to pay, let me ask you how much can/are you afford/willing to pay per month? 300…Ok Perfect, we have a package for that… ",
                                          howItWorks: "This close works by carefully tailoring finance of a product/service to fit the other persons ability or willingness to pay. ‘I cant afford this’most of the time is not an objection, its an excuse. If they really don’t want to purchase the product they will rapidly jump to other objections. Be careful when speaking to clients about finance as this subject to many people is very touchy, be aware of the words that you use because once you cross the line of finance the clients guard will go back up and you will need to put in some work to bring the barrier back down."),

                                    Close(name: "Compliment Close",
                                          technique: "One thing we can all agree on is that people love to spend money right? Nevertheless we all have a limit, some people have no problem spending a lot while other people may not want to spend that much. This doesnt take away from the fact that regardless whatever your spending you want the best for your money, now once you've been able to create value and the prospect is interest however after looking at the price they say 'As nice as it is, I simply cant afford it' - this technique comes in to play.",
                                          example: "Look I can understand that this is a bit more then you expected to pay, let me ask you how much can/are you afford/willing to pay per month? 300…Ok Perfect, we have a package for that… ",
                                          howItWorks: "This close works by carefully tailoring finance of a product/service to fit the other persons ability or willingness to pay. ‘I cant afford this’most of the time is not an objection, its an excuse. If they really don’t want to purchase the product they will rapidly jump to other objections. Be careful when speaking to clients about finance as this subject to many people is very touchy, be aware of the words that you use because once you cross the line of finance the clients guard will go back up and you will need to put in some work to bring the barrier back down.") )]

//这是我的TableView代码:

import UIKit

class BasicClosesTableViewController: UITableViewController {

var basicCloses: CloseGroup

init(basicCloses: CloseGroup) {
    self.basicCloses = basicCloses
    super.init(style: .plain)
}

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

override func viewDidLoad() {
    super.viewDidLoad()
    self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: "BasicClosesCell")
}

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

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "BasicClosesCell", for: indexPath)
    let close: Close = basicCloses.closesArray[indexPath.row]
    cell.textLabel?.text = close.name
    return cell
}

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    tableView.deselectRow(at: indexPath, animated: true)
    let close: Close = basicCloses.closesArray[indexPath.row]
    print(close.example)
}
 }

//这是我想从MVC中呈现剩余变量的地方:

import UIKit

class ExplanationTableViewController: UITableViewController {

var allCloses: CloseGroup

init(allCloses: CloseGroup) {
    self.allCloses = allCloses
    super.init(style: .plain)
}

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

1 个答案:

答案 0 :(得分:0)

嘿@Melvin Asare可能是问题仅仅是因为你的下面片段

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

请用

代替
init?(coder aDecoder: NSCoder) { 
super.init(coder: aDecoder) 
} 

希望这可以帮助你:)